javascript - Class variables outside a class -


lets have class this

class vector {     constructor(x,y) {         this.x = x         this.y = y     } } 

how can this

class vector {     constructor() {     }     var x;     var y; } var v = new vector() v.x = 3 v.y = 4 

i want declare variables outside constructor. done research. found nothing

i guess talking property initialiazers.

your code work fine if modify little:

class vector {     constructor() {}      x = 1;     y = 2; }  var v = new vector();  console.log(v.x, v.y); // 1 2  v.x = 3 v.y = 4  console.log(v.x, v.y); // 3 4 

just keep in mind stage-2 proposal, won't work in browser out of box.

if you using babel can use transform-class-properties plugin though.

if you not using babel there alternatives use - have been discussed in this question.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -