A brief analysis of variable replication parameter passing and scope chain in JavaScript

  • 2020-11-26 18:43:26
  • OfStack

In the process of reading today, I found another fuzzy point of Javascript: scope chain of JS. Therefore, I studied the content related to scope chain by looking up materials and reading books. There are several key words in today's study notes: variable, parameter passing, execution environment, variable object, scope chain.

1. The variable

There are two things to note about variables: variable declarations and copy variable values.

I'm sure you're all familiar with variable declarations. In JS, we all use the var keyword for variable declarations. JS specifies that variables declared through var are added to the nearest environment, and if a variable is declared and initialized without the var keyword, the variable is added to the global environment.

About copying variable values, because the type of variable is different, the process of copying is different. If the variable is a variable of basic type, new space will be allocated to the newly copied variable when the variable value is copied, and the two variable values will not affect each other. If the variable is a reference type, the replication operation is to make two variables point to the same memory space, and if you change one of them, the other will also change. The legend in Javascript Advanced Programming is actually pretty graphic

2. Parameter passing

Parameter passing in JavaScript is all by value. Basic types as arguments 1 are generally not confusing, if reference types are arguments, like the following example:


function setName(obj){
obj.name = "tom";
}
var person = new Object();
setName(person);
alert(person.name);// According to tom 

In this example we changed the contents of the variable in setName, which also works outside of the function. At first I thought the program execution should pop up undefined or error, but it did pop up the modified value in the scope of the function. This puzzle is solved by analyzing the whole process of parameter passing under 1. In the process of parameter passing, there is an important step: variable value replication. We actually did a step like obj=person when we called the function, so according to the reference type variable value replication mentioned above, when we changed obj, we also changed the value of person. So the JS parameter is passed by value, and only by value.

3. Execution environment, variable object, scope chain

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

Variables, functions, and other data that functions can access are defined in the execution environment, and when the execution environment is activated, a variable object is created based on the execution environment and provided to the parser for use. An execution environment is like a class, and a variable object is like an object.

When an execution environment is activated, it is pushed to the top of the stack for execution, and when it is finished, it is moved off the stack, executing the environment that was on the stack before it, and so on.

The scope chain is equivalent to a stack of variable objects. The earlier the active execution environment creates variable objects, the lower the variable objects are. The currently active execution environment's variable objects are located at the top of the stack. If the current execution environment completes, the variable object at the top of the stack (corresponding to the execution environment) needs to be moved off the top of the stack.

And execution environment when executed, the parser needs to access the variable data such as began to find the top of the scope, namely from the current execution environment begins searching for the corresponding variable object, if not find, then down into the outer execution environment corresponding variable object lookup, lasts 1 straight to find the object or found the global environment variable object. So this way of looking up also shows that there are too many variables defined in the global environment that affect the performance of the program.

Today's lesson is mainly conceptual and abstract. However, this part is the foundation for all the following knowledge, such as closure, inheritance and prototype, which can only be learned more clearly if you have a good understanding of this part. Therefore, this part should be learned over and over again, and you should believe that you can learn from the past, and the ancients will not deceive me. The & # 65381; The & # 8704; The & # 65381;) The & # 65417; The & # 65438;


Related articles: