Javascript function scope and object scope

I got to the point where I'm trying to fill my javascript knowledge with some of the more advanced concepts.

I think I really understand how the scope works. Where objects are inherited from the prototype and then the global area, while functions offer a more traditional block area within themselves.

I have a problem with understanding:

function a(){
  console.log(this.z);
  }

a.z = 12;

a(); //returns undefined :(

I expected to repeat 12, but of course it is not. Where exactly is z stored? What does "this" mean in the example?

+4
source share
1 answer

, JavaScript (this) , . , (window ) *.

, this , a. z . undefined.

12, ,

function a() {
    console.log(a.z);   // Use `a` itself, instead of `this`.
}

a.z = 12;

a();

* this undefined, - .

+1

Source: https://habr.com/ru/post/1540174/


All Articles