This talk about your understanding of the subject?

1, this always points to the direct caller of the function (rather than indirectly caller);
2, if there are new keyword, this points to the new objects out;
3, in the event, this points to an object that triggered the event, special that, IE in attachEvent in this always points to the global object
Window;
look at a few examples below, may be able to better understand this subject
point of this

this represents the current object, this point is based on the calling context to determine the default pointing to the window object
global environment,
the global environment is in the <script> </ script> inside, this here is always pointing to the window object,
<Script> Console .log (this) </ script> window object point
local environment

In the global scope direct call function, this point window
<Script>
function FUNC () {
the console.log (the this); // or the this point the window object
}
FUNC ();
</ Script>
Object function calls, which object call points to which objects

<INPUT type = "Button" ID = "btnOK" value = "the OK">
<Script>
varbtnOK = document.getElementById ( "btnOK");
btnOK.onclick = function () {
the console.log (the this); // the this points to the object btnOK
}
</ Script>
use the new instance of the object, this point in the constructor of the object is instantiated.

<Script>
var the Show = function () {
this.myName = "Mr.Cao"; // this point is where the object obj
}
var obj = new new the Show ();
</ Script>
use of this call change or apply direction


<Script>
var Go = function () {
this.address = "Shenzhen";
}
var the Show = function () {
the console.log (this.address); // output Shenzhen
}
var new new Go Go = ();
the Show. call (go); // change the method of this Show pointing objects go
</ script>

Guess you like

Origin www.cnblogs.com/crazycode2/p/12081895.html