node.js / ES6 /类创建:SyntaxError:意外保留字 [英] node.js / ES6 / class creation : SyntaxError: Unexpected reserved word

查看:256
本文介绍了node.js / ES6 /类创建:SyntaxError:意外保留字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的node.js / express应用程序上创建一个类。

I try to create a class on my node.js / express app.

它工作在基本的js / prototype模式,如:

It works in basic js / prototype mode such as :

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

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

module.exports = MyClass;

但是我想使用类,构造函数,扩展名,...关键字。

But I want to do use the class, constructor, extends, ... keywords.

我尝试过:

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

    myMethod() {
        /* method code */
    }

}

但它不起作用,错误是:

But it doesn't work, the error is :

class MyClass {
^^^^^
SyntaxError: Unexpected reserved word

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

My command line to launch the app with all harmony options :

node `node --v8-options | grep harmony | cut -d ' ' -f | xargs` my-app.js 

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

An idea to launch my app correctly please ?

推荐答案

您可以使用 io。 js

iojs --use_strict --harmony_classes my-app.js

或者在 traceur

var traceur = require('traceur');
traceur.require.makeDefault(function(file) {
  return file.indexOf('node_modules') == -1;
});

require('./my-app').run();

确保在使用新功能之前测试新功能,有些不支持。
编辑:您可以从此处查看兼容性列表

Make sure to test the new features before using them, some are not supported. You can check the compatibility list from here

这篇关于node.js / ES6 /类创建:SyntaxError:意外保留字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆