Read Uncle Tom's "JavaScript variable object" note

Source: Internet
Author: User

   A simple JavaScript code thinking

First look at a simple code, print the result is?? Why??

From the above printing results are not difficult to see, in the Print basic variable num, function expression FN, function declaration fun, we already know the variable num, function expression fn value is undefined, function declaration Fun Value is function

But is there a question? JS has a value when printing (variables, function expressions, function declarations) before the code executes, so what exactly does it do before printing (variables, function expressions, function declarations) ?

"The answer is a variable object, 2 stages of executing the context code "

(Of course the results are self-explanatory after the basic variable num, the function expression FN, and the function declaration Fun)

variable Objects variable object

If the variable is related to the execution context , then it should know where its data is stored and how it is accessed . This mechanism is known as a variable object (variable objects).

It is entirely possible for a variable object to behave in the form of a normal ECMAScript object.

VO = {};

Vo is the property of the execution context

Activeexecutioncontext = {  VO: {    //-    all formal parameters    of context data (Var, FD, function arguments) arguments function Declaration of the Functiondeclaration function    variable Declaration variable declaration     }};
2 stages of handling context code
      1. Enter execution context (before code executes)
      2. Execute code

Changes to variable objects are closely related to these two phases

(Personal understanding of label variable objects and execution context emphasis is the change of variable objects in the process before and after code execution)

To better understand the execution context staged processing (including entering the execution context, executing the code) Let's look at the following test function execution context staged process

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

In line 5th of the code above, when the test activation function is called before the 3rd line of code (including the 3rd line) , the X value is undefined and the B value is

The corresponding variable object is simply represented as follows

Variable object of the global Contextvo (globalcontext) = {  a:10,  test:};//Variable object of the "test" func tion contextvo (Test functioncontext) = {  x:30,  b:undefined};

After executing the 3rd line of code of the test function, the function test does not eject the stack, its corresponding variable object is as follows

Variable object of the global Contextvo (globalcontext) = {  a:10,  test:};//Variable object of the "test" func tion contextvo (Test functioncontext) = {  x:30,  b:20};

Because VO is not directly accessible in the context of function execution, the role of Vo is played by the active object (Activation object, abbreviated AO) .

(Callout Here activation can understand the call function, the reality of more image examples into the water into the stones, a stone stirred thousand layers of waves)

VO (functioncontext) = = = AO;  

And the properties of the execution context

Activeexecutioncontext = {  VO: {}}

so the above function executes the variable object in context represents Code modified to the following form

Variable object of the global Contextactiveexecutioncontext = {     ///Variable object of the global context     AO (g Lobalcontext) = {       a:10,       test:      };     Variable object of the "test" function context     VO (Test Functioncontext) = {       x:30,       b:20     };}

Variable Object vo(Variable object) is the property of the execution context

Variables in a variable object in the execution context the default order variable declaration follows the function declaration and the formal parameter declaration in order

Activeexecutioncontext = {  VO: {    //] Context data (Var, FD, function arguments)    All formal parameters of the arguments function ( If we are in the context of a function execution                  (a property of a variable object, this property consists of the name and value of a formal parameter, and if no actual argument is passed, then this property consists of the name of the formal parameter and the undefined value)    FD all function declarations ( Functiondeclaration, FD) a property of a variable object that consists of the name and value of a function object (Function-object), and this property is completely replaced if the variable object already has a property of the same name.    VD all variable declaration (VAR, variabledeclaration)        A property of the variable object, which consists of the variable name and the undefined value, if the variable name is the same as the formal parameter or function that has already been declared, The variable declaration does not interfere with such properties that already exist.  }};

Code embodies

alert (x); Functionvar x = 10;alert (x); 10x = 20;function x () {};alert (x); 20

Uncle Tom . In -depth understanding of the JavaScript series (12): Variable objects (Variableobject)

English Address: ecma-262-3 in detail. Chapter 2.Variable Object
Chinese address : [javascript]ecma-262-3 in-depth analysis. Chapter Ii. Variable Object

Read Uncle Tom's "JavaScript variable object" note

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.