相关疑难解决方法(0)

node.js/ES6 /类创建:SyntaxError:意外的保留字

我尝试在我的node.js/express app上创建一个类.

它适用于基本的js/prototype模式,例如:

function MyClass() { 
    /* constructor code */
};

MyClass.prototype.myMethod = function() {
    /* method code */
};

module.exports = MyClass;
Run Code Online (Sandbox Code Playgroud)

但我想使用class,constructor,extends,...关键字.

我试过了:

class MyClass {
    constructor() {
        /* constructor code */
    }

    myMethod() {
        /* method code */
    }

}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,错误是:

class MyClass {
^^^^^
SyntaxError: Unexpected reserved word
Run Code Online (Sandbox Code Playgroud)

我的命令行用于启动具有所有和声选项的应用程序:

node `node --v8-options | grep harmony | cut -d ' ' -f | xargs` my-app.js 
Run Code Online (Sandbox Code Playgroud)

想要正确启动我的应用程序的想法吗?

javascript node.js express ecmascript-harmony ecmascript-6

15
推荐指数
2
解决办法
2万
查看次数