JS scope chain

Source: Internet
Author: User

I have read and summarized some of my experiences in the rhino book over the past few days. I 'd like to take a note of it, mainly at the conceptual level. js experts can ignore it.

Variable Scope
The scope of variables is actually very simple: the local variables defined in the function are only valid in this function, and the variables defined in all functions are in JSCodeGlobal limitations, called global variables. In a special case, if the names of global variables and local variables are duplicated, the priority of local variables is higher than that of global variables. Why? I will explain it later.

Variables and Object Attributes
When learning JavaScript, you will find that the usage of variables is very similar to the object attributes, such as the value assignment, which is already used in expressions, you can even regard every variable in JS as an object property.

VaR A = 10;
VaR sampleobj = {value1: 10; value2: 20}
Sum = a + sampleobj. value1 // The calculation result is 20 because when the javascript interpreter runs, it always creates a global object first. The global variable you define is actually the attribute of this global object.

The local variables defined in the function are also the attributes of the object. This object is special and called a call object. Each time the javascript interpreter executes a function, it creates an execution environment for the function, and this execution environment creates a call object. Different function execution environments are independent of each other, while code execution environments outside all functions are called Global execution environments.

Now let's look back at it. A global object is created before the JS interpreter interprets the code. Before the code is executed to the function, the code is in the global execution environment. When the interpreter executes the function, enter the independent execution environment of the function, and create a call object. Defining a variable in the function is equivalent to defining an attribute of the call object. Can you understand it now?

Variable scope chain
This article will soon explain the priority of local variables and global variables, but you must first understand the concept of the scope chain. "Each javascript execution environment has a scope chain associated with it ." This is an expression in the rhino book. translating it into the vernacular is the priority order of the scope.

Scope chain:
To query the value of variable X, crui first queries the first object of the scope chain. If the object has an attribute named X, it uses this value. If no value exists, query the second object in the scope chain, and so on.

The order of the scope chain:
Depending on the execution environment, assuming that the current execution environment is in two-layer nested local functions, the current function call object is in the first place of the scope chain, and the outer function call object is in the second place of the scope chain, then the global object is in the third place.

Therefore, the local variables defined in the function must have a higher priority than the global variables with the same name.

 

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.