This in Javascript points to

Source: Internet
Author: User

1, as a method invocation of an object, this value is the current object

   var obj={         name:"Xiao",          getName () {
this. Name //xiao } }
Obj.getname ();

2, as a normal function call this point to window, when a function body, self-executing, the inside of this point to the window, if you want to change this can be var = this

  var obj={         name:"Xiao",          getName () {            this.name   // Xiao   }  }    var fn = obj.getnamefn (); // undefined

3. When a constructor is called, it refers to the object of new

function FN (name) {     this. Name=name       return  {        name:' bb'    } }var obj =new fn (' gg ') obj.name//  BB This  refers to the returned object 

There is another situation if the constructor returns an object when it is displayed, then this point points to the returned object instead of the new object

Therefore, if the constructor returns a non-object type of data, it does not cause the above problem

4. Call, apply

Both of them are forced to change this point, they all accept two parameters, the first one is this, the second is the argument,

Apply requires that the second argument is an array of arrays or an array of classes, call is a single parameter, and calls are generally used for parameter determination, and apply is used in the case of parameter uncertainties

This in Javascript is pointing to

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.