_javascript and JavaScript lexical scope explanation of NetEase JS-face Test Tips

Source: Internet
Author: User
Tags closure garbage collection
The calling object is at the front of the scope chain, and local variables (variables declared within the function with Var), function arguments, and arguments objects are all within the scope of the function-meaning they hide any attribute with the same name that is higher in the scope chain.

September 14, 2010, I went to participate in NetEase Web site engineer Recruitment, the application of JS engineer position. Lucky to participate in the written examination, and then lucky planted in the written exam, hehe. Talk less, grasp the sound of a very deep topic anew research.

The topic is probably: write out the output of the following code and analyze it

Copy Code code as follows:

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

"It's too easy!" "It was the first thought I saw on the subject, so the thoughtless answer was my fatal injury." My answer is--AA and DD, parsing: The result of the first output of a global variable, and then the local variable TT overwrites the value referenced by the global variable, so the second output is DD.

Anyone who sees me in this way will think that I am in the literacy-thinking and naïve (I think so too)!

NetEase Ah, how can be satisfied with this kind of answer!

The correct answer would be: undefined and DD.

Why was the first alert the result of undefined? To explain clearly the lexical scope of JavaScript that needs to be used.

Functions in JavaScript "run in the scope that defines them, not in the scope in which they are executed", which is an abstract and incisive summary of the authoritative guide.

The logic of JavaScript is performed by default in a global scope, such as "var tt= ' AA" in the above program section; is a global variable that defines a global scope (if the above code fragment is not excerpted from a chain of functions). The logic inside the test () function must add the scope (locality) of the test function itself in the original scope (global scope) chain--These ideas are defined in almost every language, whereas the JavaScript scope chain is special in that the letter Number of internal functions can be nested definition (this is the basis of the closure.) Note: In JS the function is the only form of code scope)

Nested internal functions can call variables of external functions (nested functions) and other nested functions (functions are data). If you call a nested function within an external function, the calling object does not change, and all data (including external functions and nested internal functions) will be collected by the garbage collection mechanism when the external function is executed-and this does not reflect the essence of the ' closure '. There is a situation in which is that JavaScript allows for the external invocation of nested internal functions, even if the nested function has been ' garbage collected '-the most common is in ' a function ' with its nested internal functions to define the response events of certain elements, when the page load is nested functions (' A function ' has been executed (garbage collected), but when the event is triggered there is still a response to the action, and the response function may also call to the final value of the variable defined in the nested function (' a function ') (not garbage collected?). )。

The knowledge and examples of closures are available for enquiries, and I do not want to describe them.

The focus of this article is on the following very important details:

The calling object is at the front of the scope chain, and local variables (variables declared within the function with Var), function arguments, and arguments objects are all within the scope of the function-meaning they hide any attribute with the same name that is higher in the scope chain.

That is, in the above program fragment, the "var tt= ' DD" inside the test function will cause "var tt= ' AA" to be completely hidden when the test function is invoked. Also, TT is defined after the first alert statement, so when the first alert is invoked, the TT is not yet assigned. This might make it clear that when you define the test function, when you define the first alert (TT), it records that TT is a variable in the scope chain but does not record it (TT), and that after the function is defined, TT is added to the scope. So the first alert statement can find the TT in the scope (that is, the equivalent of finding a TT that is already declared inside the function but not assigned).

The results of the above program fragment are the same as the results of the following fragment:

Copy Code code as follows:

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

The scope of JavaScript can not be simple to use C + + and other language thinking to understand Ah! C + + must be declared or defined before calling a function, and JavaScript is not necessary. You can call a function in JavaScript first, and then define it (without making any declarations before the call). Because JavaScript is the definition of a function to a scope chain when calling a function (the function runs in the scope that defines them, not in the scope in which they are executed)

As the above code is written:
Copy Code code as follows:

var tt = ' AA ';
Test (); First call and then define
function Test () {
Alert (TT); Undefined
var tt = ' DD ';
Alert (TT); Dd
}

The above code fragment although can obtain the same result, but had better not write like that, the custom is not good, the code is not good to maintain.
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.