On the _javascript techniques of variable copying, parameter transfer and scope chain in JavaScript

Source: Internet
Author: User

Today in the process of reading, and found himself at present to the JavaScript existence of a knowledge fuzzy point: JS scope chain, so by looking at the data reading on the scope chain related to the content of the study. Today's learning notes mainly have such a few keywords: variables, parameter transfer, execution environment, variable objects, scope chain.

1. Variable

Variables need to be noted for two points: variable declaration and copy variable value.

Variable declaration must be familiar to everyone, in JS we are all through the var keyword variable declaration. In JS, a variable declared through Var is added to the nearest environment, and if you declare and initialize a variable that does not use the var keyword, the variable is added to the global environment.

About copying variable values because the type of the variable is different from the process of copying it. If the variable is a basic type of variable, copying a variable value assigns a new space to the newly copied variable, and the two values do not affect each other; If the variable is a reference type, the copy operation is actually to have two variables point to the same memory space, modify one, and the other will change accordingly. The legend in the Javascript advanced programming is actually very vivid.

  2. Parameter transfer

The parameter passes in JavaScript are all passed by value. The basic type does not generally have any confusion, if the reference type does the argument, similar to the following example:

function SetName (obj) {
obj.name = "Tom";
}
var person = new Object ();
SetName (person);

In this example, we modify the contents of the variable in SetName, and it also takes effect outside of the function. At first I also thought that program execution should pop undefined or an error, but it popped the value that was modified in the scope of the function. After analyzing the whole process of parameter passing, the puzzle is solved. In the process of passing a parameter, there is an important step: the value of the variable is copied. When we call a function, we actually do obj=person such a step, so according to the characteristics of the reference type variable value mentioned above, when we modify obj, we also modify the value of person. So the way JS parameter is passed is value passing, and only value is passed.

  3. Execution environment, variable object, scope chain

My understanding of the execution environment and the execution environment is somewhat similar to that of classes and objects:

The execution environment defines other data that variables, functions, and functions can access, and when the execution environment is activated, a variable object is created for use by the parser based on the execution environment. The execution environment is like a class, and the variable object corresponds to the object.

When an execution environment is activated, it is pushed into the top of a stack, and when it is done, it is moved out of the stack, the environment in which it enters the stack before it executes, and so on.

The scope chain is the same as a stack that holds variable objects, the more the variable objects that are created in the active execution environment are below, the variable objects of the currently active execution environment are at the top of the stack. If the current execution environment is completed, you need to remove the top variable object (corresponding execution environment) from the top of the stack.

While executing the environment, the parser needs to access variables, such as data, from the top of the scope to find, that is, from the current execution environment of the variable object to start looking, if not found, then go down into the outer execution environment corresponding to the variable object to find, Continue until you find the object you want or find the variable object for the global environment. So this way of finding shows that too many variables defined in the global environment affect the performance of the program.

What we are learning today is mainly conceptual and abstract. But this part is the basis for all the knowledge behind it, like after the closure ah, inheritance Ah, prototype AH must have a good understanding of this part of the content can be learned more clearly, so this part of the content should be repeated learning, to believe that the temperature of the new, the ancients sincere not bullying me also (。 ・∀・) ノ゙

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.