ich einfach es6 Klassencode wie folgt am laufen:NodeJS Fehler auf es6 Klassenkonstruktors mit Standardparameterwert
'use strict';
class Polygon {
constructor(height=44, width=55) { //class constructor
this.name = 'Polygon';
this.height = height;
this.width = width;
}
sayName() { //class method
console.log('Hi, I am a', this.name + '.');
}
}
class Square extends Polygon {
constructor(length) {
super(length, length); //call the parent method with super
this.name = 'Square';
}
get area() { //calculated attribute getter
return this.height * this.width;
}
}
let s = new Square();
s.sayName();
console.log(s.area);
Es auf Chromkonsole läuft ok. Aber es Fehler auf NodeJS (4.x, 5.x) wie folgt ausgeführt werden:
constructor(height=44, width=55) { //class constructor
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
at node.js:405:3
Ich denke es6 für Funktionsunterstützung Standardparameter tun, und Chrom und node.js sind V8-Motor laufen, warum Sie geben diff Antwort, ...
hassansin: Danke, es funktioniert, ... Aber es ist nur auf v5.x, und es ist nicht lange stabile Version, die Produktion nicht verwenden kann, tut 4.x nicht have this option, ... – changyh
Yup, nicht alle Versionen haben dieses Flag. Hängt davon ab, welche v8-Engine es benutzt. – hassansin