Variable objects in minimalist JavaScript
Mar 21, 2021 pm 02:54 PMJavaScript Internal Power Variable Object
##Directory
- Preface
- 1. Variable object
- 2. Global variable object
- 3. Function variable object
- Write at the end
(Free learning recommendation: javascript video tutorial)
Preface
When programming JavaScript, we cannot avoid declaring functions and variables in order to successfully build our system, but how and where does the interpreter find these functions and variables? What exactly happens when we reference these objects? In the previous article "Execution Context in JavaScript" we mentioned part of it. When JavaScript code executes a piece of executable code (executable code), a corresponding execution context (execution context) will be created. For each execution context, there are three important attributes:- Variable object (VO)
- Scope chain(Scope chain)
- this
1. Variable object
In the function context, we use the activation object (activation object, AO) to represent variable objects. Active objects and variable objectsActually are the same thing:
- Variable objects are standardized or implemented by the engine and cannot be used in the JavaScript environment Access
- Only when entering an execution context, the variable object of this execution context will be activated, so it is called activation object, and only the activated variable object, that is, various variables on the active object properties can be accessed.
1 |
|
1 2 3 4 |
|
1 2 |
|
1 2 3 4 5 |
|
2. Global variable object
Let’s first understand a concept called global object. Also introduced in W3School:Global objects are predefined objects that serve as placeholders for JavaScript global functions and global properties. By using the global object, you can access all other predefined objects, functions, and properties.1. It can be referenced through this. In client-side JavaScript, the global object is the Window object.
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
variable object in the global context is the global object!
3. Variable objects in function context
In the function execution context, VO cannot be accessed directly. At this time Theactivation object (activation object, abbreviated as AO) plays the role of
VO.
1 |
|
1 2 |
|
- callee - a reference to the current function
- length — The number of parameters actually passed
- properties-indexes (integer of string type) The value of the property is the parameter value of the function (arranged from left to right in the parameter list).
- The number of elements inside properties-indexes is equal to arguments.length. The value of properties-indexes is shared with the parameters actually passed in.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
- Enter the execution context
- Code execution
- All formal parameters of the function (if it is a function context)
- 由名称和对应值组成的一个变量对象的属性被创建
- 没有实参,属性值设为 undefined
-
函数声明
- 由名称和对应值(函数对象(function-object))组成一个变量对象的属性被创建
- 如果变量对象已经存在相同名称的属性,则完全替换这个属性
-
变量声明
- 由名称和对应值(undefined)组成一个变量对象的属性被创建;
- 如果变量名称跟已经声明的形式参数或函数相同,则变量声明不会干扰已经存在的这类属性
举个例子:
1 2 3 4 5 6 |
|
在进入执行上下文后,这时候的 AO 是:
1 2 3 4 5 6 7 8 9 |
|
3.3 代码执行
在代码执行阶段,会顺序执行代码,根据代码,修改变量对象的值
还是上面的例子,当代码执行完后,这时候的 AO 是:
1 2 3 4 5 6 7 8 9 |
|
到这里变量对象的创建过程就介绍完了,让我们简洁的总结我们上述所说:
- 全局上下文的变量对象初始化是全局对象;
- 函数上下文的变量对象初始化只包括 Arguments 对象;
- 在进入执行上下文时会给变量对象添加形参、函数声明、变量声明等初始的属性值;
- 在代码执行阶段,会再次修改变量对象的属性值;
思考题
最后让我们看几个例子:
1.第一题
1 2 3 4 5 |
|
第一段会报错:Uncaught ReferenceError: a is not defined
。
第二段会打印:1
。
这是因为函数中的 “a” 并没有通过 var 关键字声明,所有不会被存放在 AO 中。
第一段执行 console 的时候, AO 的值是:
1 2 3 4 |
|
没有 a 的值,然后就会到全局去找,全局也没有,所以会报错。
当第二段执行 console 的时候,全局对象已经被赋予了 a 属性,这时候就可以从全局找到 a 的值,所以会打印 1。
2.第二题
1 2 |
|
会打印函数,而不是 undefined 。
这是因为在进入执行上下文时,首先会处理函数声明,其次会处理变量声明,如果如果变量名称跟已经声明的形式参数或函数相同,则变量声明不会干扰已经存在的这类属性。
相关免费学习推荐:javascript(视频)
The above is the detailed content of Variable objects in minimalist JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to get HTTP status code in JavaScript the easy way

How to use insertBefore in javascript
