9

I was just wondering, if it's possible to use ES6 in Node 10.15 now in 2019, because I thought, ES6 would now be a natively supported and implemented Javascript feature? I found some answer here: NodeJS plans to support import/export es6 (es2015) modules but I wasnt sure what the actual status is now.

I just tried out some ES6 classes with arrow functions in Node:

 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;
     }
 }

I get an error:

     getCounter = () => {
                ^

SyntaxError: Unexpected token =

and also, I cannot create class instance variables that are global to the class (and increase testVar by 1 every time a new class instance is created..) How is that normally done in Javascript classes?

I know there is a babel compiler package out there that supports this and transpiles the code somehow, but should ES6 not be natively supported Javascript code by now?

MMMM
  • 3,320
  • 8
  • 43
  • 80
  • 2
    `getCounter = () => {` in this context, isn't valid ES6 for a start – Jaromanda X Mar 13 '19 at 08:35
  • but it is valid *Stage 3 Proposal* - you can enable this in node using the command line flag `--harmony` – Jaromanda X Mar 13 '19 at 08:49
  • @JaromandaX is that method added to `Test.prototype` or every instance of the class directly? – adiga Mar 13 '19 at 08:58
  • @JaromandaX with --harmony flag it works with the arrow functions. – MMMM Mar 13 '19 at 09:02
  • yes, I know that @user2774480 ... but that still isn't **ES6** i.e. `ES2015 (6th edition)` ... it may be `ES2019 (10th edition)` or later, because it's not in `ES2018 (9th edition)` - ECMA did not stopped developing javascript in 2015 ... there's been 3 more specs released since – Jaromanda X Mar 13 '19 at 09:09

2 Answers2

4

Can I use ES6 Javascript in Node.js without Babel?

Yes, you can, Node supports all JS (ECMAScript) features up to ES2018 : https://node.green/

You should create your methods like this :

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;
  }
}

No need to create an attribute for the only purpose being holding an anonymous arrow function.

Seblor
  • 6,947
  • 1
  • 25
  • 46
  • aren't class fields only enabled with a command line flag? (hint: yes, they are) – Jaromanda X Mar 13 '19 at 08:48
  • ok thanks for clarifying that, I thought I had seen that kind of arrow syntax inside classes somewhere... but can you clarify more why class fields are only enabled with a command line flag? Why can't I create normal class fields? – MMMM Mar 13 '19 at 08:49
  • because they are a *stage 3 proposal* - not currently in the ecma spec – Jaromanda X Mar 13 '19 at 08:50
  • ok thanks for the info, so there are no class variables like e.g in Java? How would I increase a global class counter variable then? – MMMM Mar 13 '19 at 09:01
  • Do you mean static properties ? – Seblor Mar 13 '19 at 09:02
  • hmmm yes I probably mean static properties oops sorry ;) They are called class variables in Java I think... – MMMM Mar 13 '19 at 09:03
  • `all JS (ECMAScript) features up to ES2018` - well, that's almost true ... *proper tail calls* still fail - and they are a ES6 feature (but only safari and ios browser seem to have cracked them - surprising since I thought safari was now chrome webkit based) – Jaromanda X Mar 13 '19 at 09:27
  • @JaromandaX True, but this is an exception. And AFAIK this is an optimisation, not a feature (I may be wring I haven't looked that much into this). – Seblor Mar 13 '19 at 09:39
  • @user2774480 I know it's possible to have a "static" property by replacing `this.yourProp` by `Test.prototype.yourProp`, but I do not know if there is a better way of doing it. – Seblor Mar 13 '19 at 09:41
  • 1
    @Seblor - I wasn't being pedantic ... ok, I was :D – Jaromanda X Mar 13 '19 at 09:43
0

as of Nodejs 11.11.0, class fields are not part of the ECMAscript spec .... yet .. they are a stage 3 proposal

You can enable some (all?) of such proposed features by using the --harmony command line argument

Looking at the current state of play, class fields don't seem to be part of ES2019 (10th edition) either. Whilst I can't find that document (can only find up to ES2018 (9th edition), the table in https://node.green/ shows class fields in a section titled ESnext which is after the section ES2019

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87