Home  >  Article  >  Web Front-end  >  A brief analysis of variable copying, parameter passing and scope chaining in JavaScript_javascript skills

A brief analysis of variable copying, parameter passing and scope chaining in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:20:161190browse

While reading a book today, I discovered a vague point in my current knowledge of Javascript: the scope chain of JS, so I studied the content related to the scope chain by looking up information and reading books. Today's study notes mainly include the following keywords: variables, parameter transfer, execution environment, variable objects, and scope chains.

1. Variables

There are two things to note about variables: variable declaration and copying variable values.

Everyone must be familiar with variable declaration. In JS, we all declare variables through the var keyword. JS stipulates that variables declared with var will be added to the nearest environment. If a variable is declared and initialized without using the var keyword, the variable will be added to the global environment.

Regarding copying variable values, the copying process is different depending on the type of the variable. If the variable is a basic type variable, new space will be allocated to the newly copied variable when the variable value is copied, and the two variable values ​​​​do not affect each other; if the variable is a reference type, the copying operation actually makes two The variables point to the same memory space. If one of them is modified, the other one will also change accordingly. The illustrations in "Javascript Advanced Programming" are actually very vivid.

 

2. Parameter passing

All parameters passed in JavaScript are passed by value. There is generally no confusion when using basic types as parameters. If reference types are used as parameters, it is similar to the following example:


function setName(obj){
obj.name = "tom";
}
var person = new Object();
setName(person);
alert(person.name);//显示tom 
In this example, we modified the content of the variable in setName, and it also took effect outside the function. At first, I thought that undefined or an error would pop up when the program was executed, but the value modified in the scope of the function popped up. After analyzing the entire process of parameter passing, this doubt was solved. In the process of parameter passing, there is a very important step: copying the variable value. When we call the function, we actually perform the step of obj=person. Therefore, according to the characteristics of copying reference type variable values ​​mentioned above, when we modify obj, we also modify the value of person. So the way JS parameters are passed is by value, and it can only be passed by value.


 

3. Execution environment, variable objects, scope chain

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


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


When an execution environment is activated, it will be pushed to the top of a stack for execution. When it completes execution, it will be removed from the stack and the environment that entered the stack before it will be executed, and so on.


The scope chain is equivalent to a stack that stores variable objects. The earlier the execution environment is activated, the lower the variable object created by it is. The variable object of the currently activated execution environment is at the top of the stack. If the execution of the current execution environment is completed, then the variable object on the top of the stack (corresponding to the execution environment) needs to be removed from the top of the stack.


When the execution environment is executing, the parser needs to access variables and other data from the top of the scope, that is, starting from the variable object corresponding to the current execution environment. If it cannot be found, it goes down to the outer layer. The search is performed in the variable object corresponding to the execution environment until the required object is found or the variable object of the global environment is found. Therefore, this search method also shows that too many variables defined in the global environment affect the performance of the program.


What I learned today is mainly conceptual and relatively abstract. But this part is the basis for all subsequent knowledge, such as closures, inheritance, and prototypes. Only by having a good understanding of this part of the content can we learn more clearly, so this part of the content should be studied repeatedly. , we must believe that by reviewing the past, we can learn the new. The ancients will not deceive me (。・∀・)ノ゙


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn