Scoping and scope chain resolution in JavaScript

Source: Internet
Author: User

Learning js, certainly to learn the scope, JS scope and other mainstream language scope there is still a big difference.

A. JS has no block-level scope.

JS has no block-level scope, just like this:

if () {    var  -
// Output

JS in the words like if,for,switch, they contain the code block inside the variable, outside the code block can also be read, so, JS does not have block-level scope.

Two. JS Global Variables

JS specifies that global variables can be viewed as properties of window, and that global variables can be read by all blocks of Code.

  

var Ten ; function () {
b =; // output of ten;}
Console.log (b); Output 20;

Although a is not defined in an anonymous function, because A is a global variable, any other block of code can read the value of A. In a complex project, global variables are likely to cause significant bugs if they are inadvertently manipulated. So when writing code in peacetime, should try to avoid the use of global variables! For a variable, if it is not declared with var, then it is automatically considered a global variable, so in writing, you must not miss Var.

Three. JS Local Variables

JS in the global variables, it is easy to make code problems, so we should clearly distinguish between global variables and local variables! The local variable is read only inside his function and cannot be read outside the Function.

  

function dosomething () {    var blogname=" profile Capital ";    function Innersay () {        alert (blogname);    }     // undefined // undefined

Four. Js's Scope Chain problem

Because JS has global variables and local variables, in the call of a variable is, the scope of his chain to find, if the function defines the variable, then take the value of the variable, if not, then up a layer to find, if found, get this value, if not found, continue to find the top, until the location, If it is not found at the end, the value of the variable is Undefined.

Let's look at an example:

  

  var name = ' Chi Xuan Capital '; function scoap () {     console.log (name);      var name = ' Zhixuan ';     Console.log (name);     Console.log (age);} Scope ();

First Analyze this example, scope () will call this function, The first Console.log (name), will be the name of the value of the prototype chain lookup, first look at the function Scoap internal is defined, found within the function of the name is defined, then the first Console.log (name) will no longer find the upper layer! So is the value of the first Console.log (name) "zhixuan"? No! No! No! Because the first Console.log (name) is not assigned a value for name, the first console.log (name) is undefined and the second Console.log (name) is "zhixuan"!

Let's look at an example:

  

var a = ten; function Zhixuan () {    console.log (a);} function ziben () {    var a = +;    Zhixuan ();} Ziben ();

What is the value of Console.log (a) this time? First execute the Ziben () function, which defines a as 20, and then executes the Zhixuan () function, which requires the value of output a, because the scope is determined at the moment of the function definition, so the Zhixuan () function looks up to A's global variable, var a= 10, instead of playing the scopes in Ziben () find! So Console.log (a) is 10.

of course, My understanding of these is relatively shallow, if you want to continue in-depth, recommended Reading:

Http://www.laruence.com/2009/05/28/863.html

Http://www.cnblogs.com/lhb25/archive/2011/09/06/javascript-scope-chain.html

Scoping and scope chain resolution in JavaScript

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.