JS understanding of this

In JS, this is a dynamic binding, and the java language in other object-oriented, meaning this keyword is clear and specific, they refer to the current object, called a compiler-bound

JS in this run-bound

At this point, this may be global variables, the current object or any object - but it all depends on function is called

Several invocation:

  1. As the object to call a function as an object method call, then this point to the superior object

  2. As a function call, purely a function call, then this represents the global, usually window

  3. As a constructor call. The so-called constructor, is this function generates a new object (object) by this time, this would refer to the new object

  4. Apply and use call to call, because the JS functions are objects, naturally has a method, it is to apply and call function object. They allow switching function context- bound object context of the implementation of this that is

Reason this is so rich in meaning: JS function of both can be used as a normal function execution can be performed as an object method

When a function is executed,

  • First, create an execution environment ExecutionContext

       函数的所有行为均发生在此执行环境中
    
      构建该执行环境时,JS首先会创建arguments变量,它包含了调用函数时传入的参数,
    
  • Then create scope chain.

  • Initialize variables

     首先初始化函数的形参表,其值为arguments变量中对应的值(若arguments中没有对应值,则该形参初始化为undefined)
    

prototype of this does not change

Reproduced in: https: //my.oschina.net/boogoogle/blog/546108

Guess you like

Origin blog.csdn.net/weixin_33816946/article/details/92043739