use*_*480 5 javascript node.js ecmascript-6
我只是想知道,是否有可能在2019年的Node 10.15中使用ES6,因为我认为ES6现在将是本机支持和实现的Javascript功能?我在这里找到了一些答案:NodeJS计划支持导入/导出es6(es2015)模块, 但是我不确定现在的实际状态。
我刚刚在Node中尝试了一些带有箭头功能的ES6类:
class Test {
testVar = 1;
constructor(x,y) {
this.counter =0;
this.x = x;
this.y = y;
this.increaseCounter();
this.testVar +=1;
}
getCounter = () => {
console.log("Counter:", this.counter);
}
increaseCounter = () => {
this.counter += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
getCounter = () => {
^
SyntaxError: Unexpected token =
Run Code Online (Sandbox Code Playgroud)
而且,我无法创建对该类全局的类实例变量(每次创建新的类实例时,都将testVar增加1。
我知道那里有一个babel编译器软件包来支持此功能并以某种方式转译代码,但是ES6现在不应该被本地支持吗?
我可以在没有 Babel 的情况下在 Node.js 中使用 ES6 Javascript 吗?
是的,可以,Node 支持 ES2018 之前的所有 JS (ECMAScript) 功能:https://node.green/
您应该像这样创建您的方法:
class Test {
testVar = 1;
constructor(x, y) {
this.counter = 0;
this.x = x;
this.y = y;
this.increaseCounter();
this.testVar += 1;
}
getCounter() {
console.log("Counter:", this.counter);
}
increaseCounter() {
this.counter += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
无需仅出于保存匿名箭头函数的目的而创建属性。
| 归档时间: |
|
| 查看次数: |
1809 次 |
| 最近记录: |