By looking at Node's official website at the docs section which references ECMA6, I've learned that ecma6 features are split into three groups, namely:
- shipping
- staged
- in progress
And in the shipping category (which require no runtime flag (like harmony)), classes are featured, but with an added description (strict mode only).
I was unable to find an online resource where it is clearly stated whether NodeJS v 0.12 (the one which I am running) has support for ECMA6 classes or not.
So, following the description from the above mentioned page, I tried to include "use strict" in my js file, both at the beginning of the file and also as an alternative inside the class' scope, before any method or property declaration. However, when I run node whatever.js I get SyntaxError: Unexpected reserved word, referencing to the "class" keyword.
Some other web resources claim that to solve the reserved word error, just using the --harmony flag would help. I tried it also, but without any success whatsoever. When I run node --v8-options | grep harmony I get the following list:
--harmony_scoping (enable harmony block scoping)
--harmony_modules (enable harmony modules (implies block scoping))
--harmony_proxies (enable harmony proxies)
--harmony_generators (enable harmony generators)
--harmony_numeric_literals (enable harmony numeric literals (0o77, 0b11))
--harmony_strings (enable harmony string)
--harmony_arrays (enable harmony arrays)
--harmony_arrow_functions (enable harmony arrow functions)
--harmony (enable all harmony features (except proxies))
and as far as I'm concerned, classes are not included in this list.
I've also tried to install babel-node and run the same code from the CLI and still no success after receiving the same SyntaxError.
My question is, do you have any suggestions on what would be the most practical way to use ECMA6 classes in NodeJS for testing purposes and preferably without the need to install some compilers like traceur for instance. Ideally, I would like a clarification why Node's documentation page states that the class feature is enabled in strict mode only but I still receive the error when using strict.
Just in case:
//tried here "use strict";
class Cat {
//or here "use strict";
constructor(name) {
this.name = name;
}
speak(){
console.log(this.name + 'makes a noise.');
}
}
class Lion extends Cat {
//and here "use strict";
speak(){
super.speak();
console.log(this.name + 'roars');
}
}
const animal1 = new Cat('cat');
const animal2 = new Lion('lion');
console.log(animal1);
console.log(animal2);
EDIT (for any of you who will eventually stumble upon this question): I Have managed to solve the problem, by upgrading to the latest version of NodeJS (which is at the moment of writing 5.0.0). This page has a neat explanation on how to perform the upgrade properly. Now, when including "use strict" at the beginning of the file, the program runs error-less and I receive the expected output.