"You do not know JavaScript (on)," notes - on this

The scope of this points to a function is a common misconception, this point is not under any circumstances lexical scope of the function. Within the JavaScript, and indeed the scope of similar objects, visible identifiers are its property. But the scope of the "object" can not be accessed via JavaScript code that exist within the JavaScript engine .

function foo() {
    var a = 2;
    this.bar(); // 调用 bar() 最自然的方法是省略前面的 this, 直接使用词法引用标识符。你不能使用 this 来引用一个词法作用域内部的东西。
}

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

foo(); // ReferenceError: a is not defined

Whenever you want to find this and lexical scope of mixed use, be sure to remind myself that this can not be achieved.

Location binding and function declarations this has nothing to do, depends only on the function is called.

When a function is called, it creates an activity record (sometimes called the execution context). The record will contain the function is called (call stack) where information call the method function, passing the parameters. this is the record of one of the properties, it will be used in the course of function execution.

Guess you like

Origin www.cnblogs.com/simpul/p/11027206.html