JavaScript Scopes and scope chains

Source: Internet
Author: User

One, JavaScript scope

1 scope (global scope)

Objects that can be accessed anywhere in the code have global scope, typically with global scope in the following situations:

(1) The outermost function and the variables defined outside the outermost function have global scope, for example:

var authorname= "Mountain Side Creek"; function dosomething () {     var blogname= "Dream Sky";     function Innersay () {         alert (blogname);     }     

(2) All variables that are directly assigned to the last definition are automatically declared to have global scope, for example:

function dosomething () {     var authorname= "mountain Side Creek";     Blogname= "Dream Sky";     

The variable blogname has a global scope, and AuthorName cannot be accessed outside the function.

(3) All Window object properties have global scope

In general, the built-in properties of the Window object have global scope, such as Window.name, Window.location, Window.top, and so on.

2 scope (local scope)

In contrast to the global scope, local scopes are generally accessible only within a fixed code fragment, the most common of which is inside the function, and in some places it is also seen that this scope is called a function scope, such as the Blogname in the following code and the function Innersay only have local scope.

function dosomething () {     var blogname= "Dream Sky";     function Innersay () {         alert (blogname);     }     

Ii. scope Chain (scope Chain)

In JavaScript, functions are objects , and in fact, everything in JavaScript is an object. function objects, like other objects, have properties that can be accessed through code and a series of internal properties that are accessible only by the JavaScript engine. One of the internal properties is [[Scope]], defined by the ECMA-262 Standard third edition, which contains a collection of objects in the scope of the function being created, called the scope chain of the function, which determines which data can be accessed by the function.

When a function is created, its scope chain is populated with data objects that can be accessed by creating the scope of this function. For example, define a function such as the following:

function Add (num1,num2) {     var sum = num1 + num2;     

When the function add is created, it fills a global object in its scope chain, which contains all the global variables, as shown (note: The picture shows only a subset of all variables):

The scope of the function add will be used at execution time. For example, execute the following code:

var total = Add (5,10);

Executing this function creates an internal object called the runtime context (execution context), which defines the environment at which the function executes. Each run-time context has its own scope chain, which is used for identifier resolution, when the runtime context is created, and its scope chain is initialized to the object contained by [[Scope]] of the currently running function.

These values are copied into the scope chain of the run-time context, in the order in which they appear in the function. Together they form a new object called the "Activation Object", which contains all the local variables, named arguments, parameter sets, and this of the function, which is then pushed into the front of the scope chain, and the active object is destroyed when the run-time context is destroyed. The new scope chain is as follows:

During the execution of a function, each time a variable is encountered, it undergoes an identifier parsing process to determine where to get and store the data. The process from the scope chain head, that is, starting from the active object search, find an identifier of the same name, if found to use this identifier corresponding to the variable, if not found to continue to search the scope chain of the next object, if the search all objects are not found, the identifier is considered undefined. Each identifier undergoes such a search process during the execution of the function.

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

JavaScript Scopes and scope chains

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.