Javascript SyntaxError:尝试导出es5模块中的类以用于另一个模块时出现意外标记{

Javascript SyntaxError:尝试导出es5模块中的类以用于另一个模块时出现意外标记{,javascript,ecmascript-5,Javascript,Ecmascript 5,知道错误是如何发生的吗?类看起来被声明为ok 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 = requi

知道错误是如何发生的吗?类看起来被声明为ok

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)
Car.prototype.setNumberOfWheels(numberOfWheels){
^
SyntaxError:意外标记{
在createScript上(vm.js:80:10)
在Object.runInThisContext(vm.js:139:10)
在模块处编译(Module.js:617:28)
在Object.Module.\u extensions..js(Module.js:664:10)
在Module.load(Module.js:566:32)
在tryModuleLoad时(module.js:506:12)
在Function.Module.\u加载(Module.js:498:3)
at Module.require(Module.js:597:17)
根据需要(内部/module.js:11:18)
在对象上。(/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);
}

您应该将
setNumberOfWheels
print
声明为函数

Car.prototype.setNumberOfWheels = function(numberOfWheels) {
    this.numberOfWheels = numberOfWheels;
}

Car.prototype.print = function() {
    console.log(this);
}

使用(在对象初始值设定项上)声明方法的正确方法是:

然后添加方法,

Car.prototype.setNumberOfWheels = numberOfWheels => {
    this.numberOfWheels = numberOfWheels;
}

Car.prototype.print = () => {
    console.log(this);
}

使用(在对象初始值设定项上)声明方法的正确方法是:

然后添加方法,

Car.prototype.setNumberOfWheels = numberOfWheels => {
    this.numberOfWheels = numberOfWheels;
}

Car.prototype.print = () => {
    console.log(this);
}

在Ecmascript-5中,必须将setNumberOfWheels和print定义为函数()

你可以用


在Ecmascript-5中,必须将setNumberOfWheels和print定义为函数()

你可以用


this.number of wheels,从何处调用变量?这不是定义方法的正确方法。这一行实际上是一个语法错误。如果你想将该属性设置为函数,你必须在赋值表达式中使用右边的函数实例化表达式。是的,看着@omri_saadon answer,我意识到我错过了`=函数()`当声明函数this.number of wheels时,您从何处调用变量?这不是定义方法的正确方法。该行实际上是一个语法错误。如果您想将该属性设置为函数,则必须在右侧带有函数实例化表达式的赋值表达式中执行。是的,请查看@omri_saadon回答,我意识到我在声明函数时遗漏了`=function()`这是专门标记的。这是专门标记的。
class Car {
    setNumberOfWheels(numberOfWheels) {
        this.numberOfWheels = numberOfWheels;
    }
    print() {
        console.log(this);
    }
}


module.exports.Car = Car;