This generally means the owner of the current code.
A little plum:
<Type= "Text/javascript"> function Sayhi () {alert (var person = {}; Person.sayhello = Sayhi; Person.sayhello (); script>
Sayhi () //Kevin Yang
Person.sayhello () //name is undefined
The This at this point in the Sayhi () function represents the window or global , with the Name property under Window
The This generation in Person.sayhello () refers to the person object, with no Name attribute in person
In this article, three pointers use error conditions:
function events in 1.dom
<script type = "Text/javascript" > function sayHi () {alert ( "current clicked element is" + this.tagname); } script><input id= "btntest" type= "button" value= "click Me" onclick= "Sayhi ()" >
will give an error because this at this point in the number of rows refers to window instead of Dom
2. Temp variable causes this to lose
<script type = "Text/javascript" > var Utility = {Decode:function (str) {return unescape (str);}, Getcookie:function (key) {//... Omit the code that extracts the cookie string var value = "I%27m%20a%20cookie"; return this.decode (value);}}; Alert (Utility.getcookie ( "identity") script>
/span>
This is possible, but writing is wrong
<Type= "Text/javascript"> var getcookie = Utility.getcookie; alert (GetCookie (" Identity ")); } showuseridentity (); script>
GetCookie is a temporary variable, the pointer in the GetCookie refers to the window, so it will error
<Type= "Text/javascript"> var getcookie = Utility.getcookie;
Alert (Getcookie.call (Utility,"identity")); Alert (getcookie.apply (utility,["Identity"));
Script>
Note that JavaScript is all about value passing, without the concept of reference passing.
<Type= "Text/javascript"> var person = {Name:"Kevin Yang", Sayhi:function () { Alert ("Hello, I am" +this.name);}} setTimeout (person.sayhi,5000); Script>
Window.settimeout in this point to window
var boundfunc = Person.sayHi.bind (person,person.sayhi); SetTimeout (boundfunc,5000);
The use of this in JS