JavaScript this points to the problem

Source: Internet
Author: User

The point of this

When the function is created, the point of this is not determined, it finally points to the object that called it

Window.onload=function () {
Window.a= "My past."
Function Da () {
Console.log (THIS.A)
}
Da () //My once
}

Solution: Window calls the DA () function, so this is a pointer to the window

Window.onload=function () {
Window.a= "My past."
var bn={
A: "Little white",
Da:function () {
Console.log (THIS.A)
}
}
Bn.da () //Small white
}

Solution: The Bn object calls the DA () function, so this points to the window

Window.onload=function () {window.a= "my once" var a= "small white" var bn={        A: "White",        da:{            //a: "Pack",            B: function () {                console.log (THIS.A)}}}        bn.da.b () //undefined}

Solution: The DA object calls the B () function, so this points to the DA

Window.onload=function () {window.a= "my once" var a= "small white" var bn={        A: "Small white",        da:{            A: "Pack",            b:function () {                console.log (THIS.A)}}}    var hk=bn.da.b hk () //My Past }

Solution: Var hk=bn.da.b this sentence, the B () function is assigned a value of HK, so it calls the window.hk (), so this point to the window

This point of the constructor
function This () {    this. user = "Oh da";}  var New  // Oh da

Solution: The New keyword is the creation of an object instance, so a is an object, so the function this () is called by object A, then this point is the nature of object A, the object A will have user, because the new keyword copied a copy of the This () function

Function Fu () {      This.user = "Weapon";      return {};  } var a = new Fu;  //undefined
Function Fu () {this.name= "uncle"; return 25; }var a=new Fu;console.log (a.name) //Uncle

Solution: If the return value is an object, then this is the object returned, if the return value is not an object then this is an instance of the function, if return NULL, although NULL is also the object, but here this is also a pointer to the instance of the function

Call ()
var a = {    User: "HHH",    fn:function (A, b) {        //hhh        //8    }}var b = A.fn;b.call (a,3,5) ;

Solution: The call () method, whose first parameter is treated as the object of this. That is, this points to a, and the other parameters are passed directly to the function itself, B.

apply ()
var a = {    User: "HHH",    fn:function (A, b) {        //hhh        //11    }}var b = a.fn;b.apply (a,[ 3,5]);

Solution: The Apply () method has two parameters, the object used as the this and an array of arguments to pass to the function

Bind ()
var a = {    User:"hhh",    fn:function(AA,BB,CC) {        Console.log (  This//hhh        //1 2 3    }}var b = A.fn; var c = b.bind (a,1); C (2,3);

Solution: The Bind () method, which actually returns a function whose first parameter is used as the object for this. That is, this points to a, and the other parameters are passed sequentially to the function itself, B.

This refers to the app
Window.onload=function () {    var bn={        fn:function fn () {              function hj () {                Console.log (this)            }            HJ ()        },        gn:function gn () {              function hj (obn) {                console.log (obn)            }            HJ (this)        },        Bn:function Bn () {            var that=this            function hj () {                console.log (that)            }            HJ ()        }    }    Bn.fn ()    //window    bn.gn ()//gn    bn.bn ()//bn}

Normally, the function nesting function, the inside function of this point to the window, then we need him to point to nested his function when there are two methods; 1: Pass this as a parameter to the nested function, 2: Assign this to a variable, let it be called by the nested function

JavaScript this points to the problem

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.