In-depth analysis of ECMA-262-3. Chapter 2. Variable object

Source: Internet
Author: User

Introduction

In a program, we always declare variables and functions, and then successfully use them to build our system. When we reference the desired object, how does the interpreter find our data (functions, variable?

Many ECMAScript programmers know that variables are closely related to execution context.

var a = 10; // variable of the global context(function () {  var b = 20; // local variable of the function context})();alert(a); // 10alert(b); // "b" is not defined

At the same time, many programmers know that in the current version specification, independent scopes can only be created through the execution context of the "function" code type. That is to say, compared with C/C ++, the circular block in ECMAScript cannot create a local context.

for (var k in {a: 1, b: 2}) {  alert(k);}alert(k); // variable "k" still in scope even the loop is finished

What happens when data is declared? Let's take a look at more details.

Data Declaration

When a variable is related to the execution context, it should know where the data is stored and how to obtain it. This mechanism is called a variable object.

A variable object (VO) is an object related to the execution context. It stores:

 

  • Variable (var VariableDeclaration );
  • Function declaration (FD );
  • And function parameters.

Declare in the context

For example, it can be a variable object of a normal ECMAScript object:

VO = {};

As we said, VO is the attribute of the execution context:

activeExecutionContext = {  VO: {    // context data (var, FD, function arguments)  }};

Indirect reference variables (through the VO attribute name) are limited to variable objects in the Global Context (where the global object itself is a variable object, which will be involved later ). For other scopes, it is impossible to directly reference a VO. It is completely an execution mechanism.

When we declare a variable or function, we do not create a new property for VO. This property has the name and value of our variable.

For example:

var a = 10;function test(x) {  var b = 20;};test(30);

The corresponding variable object is:

// Variable object of the global contextVO(globalContext) = {  a: 10,  test:<reference to function>};// Variable object of the "test" function contextVO(test functionContext) = {  x: 30,  b: 20};

However, during the execution period (and in the Specification), the variable object is an abstract entity. Basically, in the specific execution context, VO has different names and different initial structures.

  • 5 pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next Page

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.