This keyword:
1, this points to the object that called the function
Typically, a variable, a function, is defined as the property of the window , the method's
Var info= ' Hello ';
Function Sayhi () {
This.style.color= ' Red ';
}
global variable info is actually window.info=' Hello ';
Calling Sayhi () is actually a window object in the call, that is, window.sayhi () usually omits the window , called directly. So this in this.style.color is pointing to window.
2. when instantiated with the new keyword, change this point and not point to window. Instead, it points to the instantiated object.
Example:
Function Student(){
This.name= "Lydia";
}
Var stu=new Student ();
This point points to Stu
3. Anonymous functions are global, so in anonymous functions this points to the window Object
Var name= ' outer ';
Var obj={
Name: ' Inner ',
Say:function () {
Alert (this.name); Inner This points to the object that is calling its function, obj
(Function () {
Console.log (this.name); Outer this point to window
}());
}
}
Obj.say ();
This in JavaScript points to the problem