In layman's scope: JavaScript scoped Chain

Source: Internet
Author: User

1. What is a scope
Any programming language has the concept of scope, simply speaking, scope is the scope of the variable.
2. Classification of variables and scope of variable scopes
In JavaScript, variables are divided into global variables and local variables, which correspond to the scope of the variables are global scope and local scope two kinds.
3. Global variables and global scopes

A variable declared outside a function is a global variable, and the scope of the global variable is the global scope. Look at the following code:

var globalele = "The World is so Big"; function func () {    console.log ("Inside the function:" + Globalele);} Func (); Console.log ("external function:" + Globalele);

Printing results:

Results Analysis:

A variable globalele is declared outside the function, so it is a global variable with global scope, and global variables can be accessed either inside the function or outside of the function.

4. Local variables and local scopes

A variable declared within a function is a local variable, and the scope of the local variable is a local scope. Look at the following code:

function func () {    var localele = "And I am in domestic"    Console.log ("Inside the function:" + Localele);} Func (); Console.log ("external function:" + Localele);

Printing results:

Results Analysis:

A variable localele is declared inside a function, so it is a local variable, has a local scope, local variables can only function within the currently declared functions, so accessing the local variable outside of the function will prompt not defined.

5. What is a scope chain

The process of establishing the scope chain here is not detailed, some of the official website definition of the abstract concept is not much to be elaborated, by the following example to see, look at the following code:

var ele = "variables defined outside the function"; function func () {    var ele = "variable defined within function func";     function Son () {        var ele = "variable defined in the function son";        Console.log (ele);    }    Son (); Func ();

Printing results:

The result analysis: The variable ele is defined three times, but their scope is different, the first defined ele is the global variable, has the global scope. The second definition of the ele is a local variable, which works within the Func function, because the function son is a child of function func, so the variables defined here can also be accessed within the function son. The third definition of ele is also a local variable, which works within the function son. Therefore, in the function son inside the access variable ele that is the print variable ele, will follow the nearest principle to access the function son inside the definition of the variable ele, if you can not find a layer of online search, until the top layer, if still find not to prompt not defined. Hopefully you can understand the scope chain through this simple case.

Well, say so much, the above statements have not strict point also please correct me, I wish you a happy work!

  

  

In layman's scope: JavaScript scoped Chain

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.