N
N
Nick Bukovskiy2018-03-26 17:16:45
JavaScript
Nick Bukovskiy, 2018-03-26 17:16:45

How to reach the class fields while in the method function?

class Foo {
   constructor(){
        this.defaults = '123'
   }

   init() {
      function bar(){
          console.log(this.defaults); // тут не видно
      }
  }
}


How to get the value NOT by making the function an arrow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Grishin, 2018-03-26
@sonterix

No way. But if it is enough for you that the bar() function is not called on the forehead, you need to call it private. Well, either pass the object explicitly in the parameters, and ensure the visibility of the required fields outside the class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question