An interesting JS question and a personal understanding

Source: Internet
Author: User

var number = 2;var obj = {  number:4,  fn1: (function () {         this.number *= 2;    number=number*2;    var number=3;    return function () {      this.number *= 2;      number*=3;      alert (number);    }   } ) (),  db2:function () {This.number*=2}};var fn1 = Obj.fn1;alert (number);//ask what results will pop up 4fn1 ();//What results will be ejected 9obj.fn1 () ;//What results this pop-up 27alert (Window.number); This will window.number the result is what 8 alert (obj.number);   What is the result of this Obj.number 8

This is I inadvertently see a JS problem, at that time understand a long, but finally understand, the following attached their own analysis.

First, the compilation phase

var number = 2; var obj = {  4,  function () {         this.number *= 2;    number=number*2;    var number=3;    return function () {      this.number *= 2;      Number*=3;      alert (number);    }   } ) (),  db2:function () {this.number*=2}}; 
First look at the above code, which includes a iife (immediately-invoked function expression) immediately executes the function expressions (functions () {}) (). This function is executed during the compile phase. The primary role is to isolate scopes, mimicking block-level scopes. For ease of understanding, I add alert () to the Code;
varNumber = 2;varobj ={number:4, fn1: (function() {          This. Number *= 2; Alert ( This ); WindowNumber=number*2;  alert (number); //nan Reason: variable declaration in advance    varNumber=3; Alert ( "Execution"); return function() {       This. Number *= 2; number*=3;    alert (number); }}) (), DB2:function(){ This. number*=2}};
You can see that the this of the Iife function is pointing to the window, and it executes during the compile phase, so running the above code will print in turn:windowNANExecutionand the global number=4; obj.number=4; var number=3 exists in Iffe; (because the existence of a closure causes Iife's active objects to exist) second, the execution phase
var fn1 = obj.fn1;alert (number); // ask what results this will pop up 4fn1 (); // What results will this pop up 9obj.fn1 (); // What's the result of this pop? //  alert (obj.number);   // what is the result of this Obj.number 8
1, var fn1 = obj.fn1, so that the fn1 point to the Iffe closure, the existence of the closure 2, alert (number);//At this point refers to the global number=4;3, fn1 ();//Call the FN1 () function, The principle of this in a closure is which object calls the function, and this points to which object. This is the Window object call. Thus, this.number refers to the global number (which is both a variable and a window property), the global number=8, and the number of variables, based on the scope chain and the rules of the closure, takes precedence over the internal environment (none), and then to the upper level (Var number= in the anonymous function 3) so var number=9; so the alert (number) is 9. 4, Obj.fn1 ();//obj call Fn1,obj.number=8, the number of the variable is based on the scope chain and the rules of the closure, first in the internal environment (none), and then to the upper level (Var number=9 in the anonymous function) so var number= 27; So the alert (number) is 27. 5. At this time window.number=8;6. obj.number=8; Focus: Understand the isolation scope of iife and understand the scope chain of closures.

An interesting JS question and a personal understanding

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.