Deceive lexical

JavaScript There are two mechanisms to achieve when you run the program "modified" (it can also be said to deceive) lexical scope.

 

eval


  eval JavaScript in (...) function can take a string as a parameter, and the contents treated as writing the code when it seems to exist in this location.

  According to this principle can be achieved deceive lexical. When the code after execution of eval (...), the engine does not know or do not care about the form of the previous code is dynamically inserted in, and modify the lexical scope of the environment. as usual, the only engine to find lexical scope

  For example:

  

function foo(str,a){
    eval(str);  //欺骗
    console.log(a,b)
}

var b = 2;

foo("var b =3;",1);  //1,3

  Here's eval (str) call actually var b = 3. This code will be treated the same as itself here. So it will be the engine found in the current scope that is b 3.

 

 

with

  JavaScript is another difficult to master (and now deprecated) to deceive the lexical scope of the function.

  as is typically repeated with shortcut to reference a plurality of attributes of the same object, the object itself may not need to duplicate references.

var obj = { 
    A: . 1 , 
    B: 2 , 
    C: . 3 
}; 

// tedious repetition obj 
obj.a = 2 ; 
obj.b =. 3 ; 
obj.c =. 4 ; 

// simple shortcut 
with ( obj) { 
    A =. 3 ; 
    B =. 4 ; 
    C =. 5 ; 
}

  But in fact this is not just for the convenience of accessing object properties Consider the following code:

function foo (obj) {
     with (obj) { 
        A = 2 ; 
    } 
} 

var O1 = { 
    A: . 3 
} 

var O2 = { 
    B: . 3 
} 

foo (O1); 
the console.log (o1.a); // 2 

foo (O2); 
the console.log (o2.a); // undefined 
the console.log (a); // 2 a escaping into the global scope

  You may be with or without a plurality of attributes of objects treated as a completely separate lexical scope, so this property will be treated as an object identifier defined in the lexical scope of this

 

About performance, JavaScript engine will be at the compilation stage performance optimization of several items, some of which optimization depends on being able to statically analyze accordance with this law code, and pre-definition of the location of all variables and functions, in order to quickly find the logo in the implementation process character. However, if the engine found eval and with in the code, he can only judge on the simple assumption that the location identifier is invalid because there is no longer the lexical analysis phase eval know exactly what code would receive, how the code to modify the scope, you can not know what the object with contents to be used to create new lexical scope of in the end yes.

Or if there is a eval with, all of the possible optimization is meaningless, so the easiest way is completely without any optimization.

Recommends not using these, only the knowledge to do the expansion ...

    

Guess you like

Origin www.cnblogs.com/evilr/p/11540728.html
Recommended