You don't know. JavaScript-2. Lexical scopes

Source: Internet
Author: User

Consider the following code:

function Foo (a) {    var b = A * 2;     function Bar (c) {        Console.log (A, B, c);    }     * 3//  2, 4,

In this example, there are three scopes that are nested in a hierarchical set.

    1. Contains the entire global scope, where there is only one identifier: foo.
    2. Contains the scope created by Foo, which has three identifiers: A, bar, and B.
    3. Contains the scope created by bar, where there is only one identifier: C.


Global variables automatically become properties of global objects, such as window objects in a browser, so they can be accessed indirectly through a reference to a global object's properties, rather than directly through the lexical name of the global object.
Window.a
The global variables that are obscured by local variables with the same name can be accessed through the way. But non-global variables, if obscured, cannot be accessed in any way.


Eval

The eval (..) function in JavaScript can accept a string as a parameter and treat it as if it were a code that existed at this point in the program at the time of writing. In other words, you can generate code and run it in the code you write, as if the code were written in that location.

Consider the following code:

function foo (str, a) {    //  cheat!      Console.log (A, b);} var b = 2//  1, 3

"var B = 3 in the eval (..) call;" This code will be treated as if it were there.
Since that code declares a new variable B, it modifies the lexical scope of the existing Foo (..). In fact, like the aforementioned principle, this code actually creates a variable B inside Foo (..) and obscures the variable with the same name in the outer (global) scope.


With

With is often used as a shortcut to repeatedly reference multiple properties in the same object, you can not refer to the object itself repeatedly.

// Tedious repetition of "obj"obj.a = 2= 3= 4; // a simple shortcut  with (obj) {    = 3;     = 4;     = 5;}

But in fact it's not just for easy access to object properties. Consider the following code:

function foo (obj) {    with  (obj) {    = 2;    }} var o1 = {    3}; var o2 = {    3/ / 2//  undefined  //  2--Bad, A is leaking to the global scope!    

You can notice a strange side effect, in fact a = 2 assignment operation creates a global variable A. What's going on?

It can be understood that when we pass O1 to with, the scope declared by with is O1, and the scope contains an identifier that matches the O1.a property. But when we use O2 as a scope, there is no a identifier, so a normal LHS identifier is found.

Identifier A is not found in O2 scope, Foo (..) scope, and global scope, so when a=2 executes, a global variable is automatically created (because it is non-strict mode).


Eval, with causes the engine to be unable to optimize scope lookups at compile time, which can affect performance.

You don't know. JavaScript-2. Lexical scopes

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.