1

I need to declare some properties for my class but this does not work:

class MyClass {
  var property1; //<--error raised!
  var property2;

  constructor(prop1,prop2) {
    this.property1 = prop1;
    this.property2 = prop2;
  }
}

It seems ECMAScript 6 has no class property declaration yet? I'm using NodeJS 4.x which supports ECMAScript 6.

Updated: ES6 stage 3 now has public & private properties: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Field_declarations

Dee
  • 7,455
  • 6
  • 36
  • 70
  • Note that node.js 4.x is only supporting a small subset of ECMAScript 6. https://kangax.github.io/compat-table/es6/ – birnbaum Apr 05 '16 at 13:57
  • If you are willing to add a build step you could use Babel with [a plugin](https://babeljs.io/docs/plugins/transform-class-properties/) for transforming class properties. This would allow you to use a syntax very similar to what you're currently trying. – James Allardice Apr 05 '16 at 13:59
  • 2
    The assignments in your constructor totally suffice. There's no need - and no way - to "declare" instance properties in the class in JavaScript. – Bergi Apr 05 '16 at 16:10
  • i need to reopen this question, ES6 stage 3 update has public property & private property: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Field_declarations – Dee Feb 20 '19 at 02:36

0 Answers0