For the problem of this point in JS, print out the value of OBJ.NM ()

Source: Internet
Author: User

The problem is as follows:

var num = 1;
var obj={
Num:8,
NM: (function () {
This.num *=2;
return function () {
return This.num *=2;
}
})()
}
var dbl = obj.nm;
OBJ.NM ();
OBJ.NM ();
OBJ.NM ();
Console.log (obj.num);//64
Console.log (this.num);//2

Explain:

In fact, this is the point of this problem, this point to understand the nature is clear!

First look at the first sentence:

This sentence is declared a global variable num=1; (there is nothing to explain, if this is not understood, go to the JS basis to learn it)

var num = 1;

Next, an object, obj, is created in a literal way, containing two key-value pairs;
var obj={

A common attribute of num,obj, with a value of 8;
  Num:8,

The value of NM is a closure function (self-executing function);
  NM: (function () {

Here is the point!!! The OBJ.NM () closure function, whether executed or not executed, is not called by OBJ.NM (), so this refers to window;

        Console.log (this);//window
        This.num *=2;

When executing obj.nm (), the following function is returned to obj, that is, this is the function in the inside of the reference to obj, can say who called the function, this point to who, so here this point to obj;
return function () {

          Console.log (this);//obj
return This.num *=2;
}
})()
}

This is for cheating! Execute or not execute, window.num = 2,obj.num=8; (The closure function calls itself once, so start defining the global num*2=2;)
var dbl = obj.nm;

After performing the following, the value of NM is the function of return in the closure function is executed, because the closure function is a self-executing function, obj.num () is not qualified to call it, the call is returned to Obj.num () function;

After execution window.num=2; (Obj.num () called obj.num), obj.num=16;
OBJ.NM ();

Similarly, the following obj.num () are executed obj.num*2; window.num = 2;obj.num = 32;
OBJ.NM ();

Window.num = 2;obj.num = 64;
OBJ.NM ();

At this point, the result must be window.num = 2,obj.num = 64;
Console.log (obj.num);//64
Console.log (this.num);//2, this is the window.

For the problem of this point in JS, print out the value of OBJ.NM ()

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.