preface
Relearning javascript execution context stack juejin.cn/post/695048…
Each execution context has three important properties:
- The variable object
- The scope chain
- this
This chapter takes a closer look at javascript variable objects
remain true to our original aspiration and keep our mission firmly in mind
When many students look at this, they may think: “JS is too simple, is it necessary to write?” . Indeed, relative to the beginner js of us, now review is very simple, but there is a law of nature, including our learning of front-end knowledge, that is to master the truth, return to nature, in order to sublimate on the road more solid!
The body of the
What is a variable object
- The variable object is the data scope in the execution context and stores the variable and function declarations that we define in the context
- Of course, variable objects in different execution contexts are also different.
- In general, the global context and the functional context are the ones we often deal with.
Let’s talk about global context.
Global context
What is a global object? Let’s take a look at the W3C definition.
1. Global properties and functions can be used for all built-in Javascript objectsCopy the code
Description of global objects:
- Global objects are predefined objects that act as placeholders for javascript global functions and global properties. By using global objects, you can access all the other predefined objects, functions, and properties. The global object is not a property of any object, so it has no name.
- In top-level JavaScript code, you can reference the global object with the keyword this. But you don’t usually need to refer to a global object in this way, because it is the head of the scope chain, which means that all non-deterministic variable and function names are queried as properties of the object. For example, when JavaScript code refers to the parseInt() function, it refers to the parseInt property of the global object. The global object is the head of the scope chain, which also means that all variables declared in the top-level JavaScript code will become properties of the global object.
- When JavaScript code is embedded in a particular environment, global objects usually have context-specific properties. In fact, the ECMAScript standard does not specify the type of a global object, and JavaScript implementations or embedded JavaScript can treat any type of object as a global object, as long as it defines the basic properties and functions listed here. For example, in JavaScript implementations that allow Java to be scripted via LiveConnect or related technologies, global objects are given the Java and Package properties listed here along with the getClass() method. In client-side JavaScript, the global object is the Window object, which represents the Web browser Window that allows JavaScript code.
Further explanation:
-
You can use this to reference the global object, which in client-side javascript is the Window object
-
this instanceof Object
-
Math.random() equals this.math.random ();
-
The variable defined by var is mounted on the global object
var a = 1;
console.log(a)/ / 1
Copy the code
In short: a variable object in a global context is a global object, hahahahaha!
Function context
A variable object in a function context can be called an active object.
But live objects are not the same thing as variable objects, because live objects are not accessible in the javascript environment, only implemented in the javascript engine. That is, it can only be activated when it is entered into a specific execution context. Only when the active object is activated can it become a true variable object, and then the various properties on it can be accessed.
Implementation process
- Enter execution context
- perform
Enter execution context
At this point it is an analysis process, that is, no code is being executed.
Variable objects include function parameters, function declarations, and variable declarations
example
function foo(a) {
var b = 2;
function c() {}
var d = function() {};
b = 3;
}
foo(1);
Copy the code
Enter the function execution context
AO = {
arguments: {
0: 1.length: 1
},
a: 1.b: undefined.c: reference to function c(){},
d: undefined
}
Copy the code
Conclusion:
-
The variable object initialization of the global context is the global object
-
Function context variable object initializations include only Arguments objects
-
Initial attribute values such as parameters, function declarations, and variable declarations are added to variable objects when entering the execution context
-
During code execution, the attribute values of the variable object are modified again