The use of this in JS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.