"JavaScript" 4, execution Environment, variable object and declaration in advance

Source: Internet
Author: User

This period of time has been looking at some of the JavaScript books, see not understand where the search for answers, the results of this evening in searching for a strange grammar of the time to open an interview problem, is a study of the scope of the ten old problem, so I tried to do a bit, decisive by the pit, after the end of the analysis, See also not very clear, so hurriedly go back to see the basic book, the results found before a lot of their own eyes swept the knowledge point of their own completely no master, instant regret extremely, so take advantage of quickly re-learn, at the same time these points recorded down.

The original question is this:

var tt = ' AA '; function test () {alert (TT); var tt = ' DD '; alert (TT);} Test ();

The answer is UNDEFINED,DD.

Just see the answer when feeling the pit Father Matchless, behind look analytic, feeling is also foggy, but fortunately know where to find the answer, this topic mainly involves the execution environment, variable object, declaration advance and other knowledge points, here briefly review

The execution Environment defines other data that variables or functions have access to, and determines their various behaviors. Each execution environment has a variable object associated with it . all variables and functions defined in the environment are stored in this environment. However, we cannot access this object directly.

The global execution environment is generally considered a Window object, so all global variables and functions are created as properties and methods of the window.

Each function has its own execution environment, and when the execution flow enters a function, the execution environment of the function is pushed into an environment stack , and after the function executes, the stack launches the environment and returns control to the previous execution environment.

When code executes in an execution environment, a scope chain of variable objects is created, and the role of the scope chain is to ensure an orderly access to the variables and functions that the execution environment has access to.

The most front-end of the scope chain is always the variable object of the current execution environment, followed by the variable object of the previous execution environment, followed by the variable object of the execution environment on the first layer, knowing the global environment.

If the execution environment is a function, its active object is used as a variable object, and the active object initially contains only one arguments object, and the variables and functions defined inside the function are added to the active object before the function executes.

declaration in advance means that when the parser parses code in a scope, the definition of the variable and function is advanced to the top of the scope, and the assignment is left in place.

Well, with the above theoretical knowledge, we will look at this problem again.

First, declare a global variable, TT, and re-assign a value of ' AA '.

Then, create the function test (), according to the above, the function has a own execution environment, and associated with a variable object, the variable object has a arguments object, at this time, this arguments object is null. When the parser parses the function, the variables and functions defined in the function are added to the variable object, and the variable definition statement is advanced to the top of the scope.

Then, when the function executes, the scope chain is created, the scope chain puts the function's own variable object at the forefront of the scope chain, followed by the variable object of its last-level scope, which is the Window object. At this point two variable objects have a variable TT, in the front-end function within the definition of the TT will shield off the global TT, and then according to the declaration in advance, at this time var tt= ' DD ' will be divided into two parts, the declaration part (Var TT) is advanced to the beginning of the function, and the assignment statement (tt= ' DD ') When the first alert (TT) is executed, the undefined pops up, because at this point the TT is only declared, not yet assigned, and the second alert pops up DD, because the TT is already assigned a good value.

This article is from the "Wanghaz Learning" blog, so be sure to keep this source http://10703847.blog.51cto.com/10693847/1706959

"JavaScript" 4, execution Environment, variable object and declaration in advance

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.