SyntaxError: Unexpected token { when trying to export a class in es5 module for use in another module
SyntaxError: Unexpected token { when trying to export a class in es5 module for use in another module
知道错误是如何发生的吗? class 看起来是在 ..
上声明的
car.js
function Car() {}
Car.prototype.setNumberOfWheels(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print() {
console.log(this);
}
module.exports.Car = Car;
server.js
const model = require('./model.js');
错误:
Car.prototype.setNumberOfWheels(numberOfWheels) {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/bob/git/project/server.js:2:15)
您应该将 setNumberOfWheels
和 print
声明为函数。
Car.prototype.setNumberOfWheels = function(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print = function() {
console.log(this);
}
使用 shorthand syntax(在对象初始值设定项上)声明方法的正确方法是:
var car = {}
然后添加方法,
Car.prototype.setNumberOfWheels = numberOfWheels => {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print = () => {
console.log(this);
}
在 Ecmascript-5 中,您必须将 setNumberOfWheels 和 print 定义为函数 (see prototype examples)
function Car() {}
Car.prototype.setNumberOfWheels = function(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print= function() {
console.log(this);
}
module.exports.Car = Car;
您可以使用ES6 Classes
class Car {
setNumberOfWheels(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
print() {
console.log(this);
}
}
module.exports.Car = Car;
知道错误是如何发生的吗? class 看起来是在 ..
上声明的car.js
function Car() {}
Car.prototype.setNumberOfWheels(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print() {
console.log(this);
}
module.exports.Car = Car;
server.js
const model = require('./model.js');
错误:
Car.prototype.setNumberOfWheels(numberOfWheels) {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/bob/git/project/server.js:2:15)
您应该将 setNumberOfWheels
和 print
声明为函数。
Car.prototype.setNumberOfWheels = function(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print = function() {
console.log(this);
}
使用 shorthand syntax(在对象初始值设定项上)声明方法的正确方法是:
var car = {}
然后添加方法,
Car.prototype.setNumberOfWheels = numberOfWheels => {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print = () => {
console.log(this);
}
在 Ecmascript-5 中,您必须将 setNumberOfWheels 和 print 定义为函数 (see prototype examples)
function Car() {}
Car.prototype.setNumberOfWheels = function(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
Car.prototype.print= function() {
console.log(this);
}
module.exports.Car = Car;
您可以使用ES6 Classes
class Car {
setNumberOfWheels(numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
print() {
console.log(this);
}
}
module.exports.Car = Car;