Talk about my understanding of JavaScript prototypes and closures (handy notes 9) _javascript tips

Source: Internet
Author: User
Tags variable scope

Related readings: Talk about my understanding of JavaScript prototypes and closures (handy notes 6) I understand JavaScript prototypes and closures (handy notes 8)

Scope

Reference to the scope definition in the JavaScript language Essence and Programming Practice:

The scope of the variable is also called the visibility of the variable. The variable scope completes the concealment of the information, which is to deal with the problem of "separatist".

JS is not a block-level scope (ES6 has a let, can be declared inside the {},if,for, while the scope is limited to block-level. Let declaration variable does not exist variable elevation! I don't talk about this here, because I happen to see it too. )。

When we write code, do not declare variables in "block", should be declared at the beginning of the code, so as to avoid ambiguity.

 for (var i = 0; i < i++) {//Bad declaration way
 //...
}
Console.log (i);
/*----------------------------------------------/
var i = 0;//good declarative way for
(i = 0; i < i++) {
 //....
   }

JavaScript has a function scope in addition to the global scope.

When we declare a variable, the global code is declared at the front end, and the function declares the variable to be declared in front of the function body. Also, you must use the "var" operator when declaring a variable.

 var a = ten,  //global scope
 B =;
function fn () {//FN functions scope
 var a = m,
  c =;
 function Bar () {//bar-scoped
  var a = 1000,
   d = 4000;
 }

Global code, the FN function, and the bar function all form a scope. The scope has a subordinate relationship, the subordinate relationship is to see which scope the function is created under. The bar function is created under the scope of the FN function, and the "FN function scope" is the ancestor of the scope of the bar function.

The best use of the scope is to isolate the variable, and the variable with the same name will not conflict under different scopes.

--------------------------------------------------------------------------------

The scope is determined when the function is defined, not when the function is called.

1, the program at the time of loading has determined the global context environment, and with the execution of the program on the variable on the row assignment.

2, execute to 36 lines, invoke FN (10), create the execution context of the FN function, press the stack, and set this context to active.


3, execute to 32 lines, call Bar (100), create the execution context of bar (100) function, press stack, and set this context to active state.

4, bar (100) call completed, out of the stack, bar (100) function context environment was destroyed. It then executes 33 lines, calls bar (200), creates the execution context of the bar (200) function, presses the stack, and sets the context to active.

5, bar (200) call finished, out of the stack, the context of its environment was destroyed. Control at this time is given to the FN (10) context, which becomes active again.

6, at this time the FN (10) Call completed, out of the stack, the context of its environment was destroyed. Control is given to the global execution context.

The execution of this piece of code is over here.

Then borrow a complete picture of the original author:

Summarize:

A scope is just a "site" that gets the value of a variable through the context of the execution contexts of the scope. Under the same scope, different invocations produce different execution context environments, and then produce different values of variables. Therefore, the value of the variable in the scope is determined during execution, and the scope is determined when the function is created. So, if you want to find the value of a variable in a scope, you need to find the execution context for that scope, and then find the value of the variable.

Related Article

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.