Hello!
The problem is that the constructor "does not see" the class methods.

class SimpleClass{ constructor(){ this.x = this.getX(); } getX(){ return 'some value'; } consoleX(){ console.log(this.x); } } var simpleObject = new SimpleClass(); simpleObject.consoleX(); 

Prints undefined
I can not understand why - either I'm doing something wrong, or it should be.
If it should be like this, call the class methods inside the constructor (I understand that the antipattern is, well, really necessary).
Thank!

  • I get some value. - Darth
  • 3
    This code works. Show the code with the problem - Alexey Ten
  • Hm Strange! It really works. So. Now I'll get to the office and drop it. - OO
  • Hm Yes, and in the original code everything works. What was it like that? - OO
  • Question do not persuade - I will recreate the situation and lay out - OO

1 answer 1

As we see below from the snippet, the behavior is as expected.

 class SimpleClass{ constructor(){ this.x = this.getX(); } getX(){ return 'some value'; } consoleX(){ console.log(this.x); } } var simpleObject = new SimpleClass(); simpleObject.consoleX();