xCatalog [1]eval [2]with Front words
Eval and with are often rejected as if their existence was wrong. In CSS, the table is rejected, in the Web page is only used to display data, rather than the layout, may be dismissed as nonstandard, overkill. What is the case with eval and with? The eval () function and the WITH statement are described in detail in this article
Eval definition
Eval () is a global function that JavaScript interprets by using eval () to run a string consisting of JavaScript source code
var result = eval (' 3+2 '); Console.log (result,typeof result); // 5 ' number '
Usage
Eval () has only one parameter, and if the parameter passed in is not a string, it returns this parameter directly. If the argument is a string, it compiles the string as a JavaScript code. Throws a syntax error (SYNTAXERROR) exception if compilation fails. If the compilation succeeds, the code is executed, and the last expression or the value of the statement in the string is returned, and if the last expression or statement has no value, the undefined is eventually returned. If the string throws an exception, the exception will pass the call to eval ()
var num = 1; var str = ' test '; Console.log (eval (num)); // 1console.log (eval (str)); // referenceerror:test is not defined
var strLong1 = ' var x = 1;var y = 2; ' ; Console.log (eval (strLong1), x, y); // undefined 1 2 var strLong2 = ' var x = 1; x + +; ' ; Console.log (eval (strLong2), x); // 1 2
Scope
Eval () uses the variable scope environment that called it. That is, it looks for the value of the variable and the operation that defines the new variable and function is exactly the same as the code in the local scope
var b = 2; function foo (str,a) { eval (str); Console.log (A, b);} Foo (' var b = 3; ', 1); // 1 3
Alias
When called by an alias, Eval () executes its string as the top-level global code. Executed code may define new global variables and global functions, or assign values to global variables, but cannot use or modify local variables in functions
varGeval =eval;varx = ' global ', y = ' global '; functionf () {varx = ' Local '; Eval (' x + = ' changed '; '); returnx;}functiong () {vary = ' local '; Geval (' Y + = ' changed '; '); returny;} Console.log (f (), x);//localchanged GlobalConsole.log (g (), y);//Local globalchanged
Note The Ie8-Browser invokes eval () through an alias and the result of a normal call to Eval () is the same
Side effects
The JavaScript interpreter carries out a lot of code analysis and optimization. The problem with Eval () is that code that is used for dynamic execution is usually not parsed, and the interpreter cannot optimize it, which can lead to performance degradation
Similar to eval () is settimeout (), SetInterval (), new function (), and so on, which can be executed dynamically when the program is run, using a string as an argument. The benefits of this execution mechanism cannot be offset by the loss of performance, so try to avoid using
Strict mode
Because the eval () function is too powerful, strict mode restricts it strictly.
' 1 ' cannot create a variable or function through the eval () function, but can query and change its value
' Use strict '; eval (' var x = 1; ') ); Console.log (x); // referenceerror:x isn't defined' use strict '; var x = 1; eval (' x = 2; ') ); Console.log (x); // 2
' 2 ' disables the use of eval as an identifier
' Use strict '; var eval = 10; // syntaxerror:unexpected eval or arguments in strict mode
With
The purpose of defining the with statement is primarily to simplify the work of writing the same object multiple times
The With statement adds an object to the head of the scope chain, then executes the statement and finally restores the scope chain to its original state
with (object) {statement;}
Role
The With statement is typically used to simplify code writing when the object nesting level is deep. Essentially, a new lexical scope is created by processing the object's properties as an identifier in scope by treating the object's reference as a scope.
In client-side JavaScript, you might use an expression like this to access an element in an HTML form
Document.forms[0].address.value
If this expression appears more than once in your code, you can use the WITH statement to add the form object to the top level of the scope chain
with (Document.forms[0]) { = '; = "; = ';}
This method reduces the number of inputs, and no more document.forms[0] prefixes are added for each property name. This object is temporarily mounted on the scope chain, and when JavaScript needs to parse an identifier such as address, it is automatically found in this object
[Note The]with statement provides a shortcut to read the properties of an object, but it does not create an object's properties
If the object o has an attribute x, then the following code assigns this property a value of 1
var o = {x:0}; with (o) x = 1; Console.log (o.x); // 1
If no attribute x is defined in O, the following code is identical to the code x=1 without using the WITH statement. This is because a LHS query is made on the variable x and assigns 1 to it
var o = {}; with (o) x = 1; Console.log (o.x); // undefinedconsole.log (x); // 1
Side effects
Similar to eval, the JavaScript code for the WITH statement is very difficult to optimize, but it also makes debugging code difficult, and it can be slower than code that does not use the WITH statement.
Also, if the WITH statement is inappropriate, it is possible to cause a variable leak that pollutes the global scope
var x = 1; var o = {}; with (o) { = 2;} Console.log (x); // 2Console.log (o.x); // undefined
Strict mode
Using the WITH statement is prohibited in strict mode
// syntaxerror:strict mode code may not include a with statement' use Strict '; var o = {}; with (o) { = 2;}
At last
Using eval and with will make it impossible for the engine to optimize scope lookups at compile time, resulting in degraded performance and slower code execution. Because Eval and with are seldom used in the actual work, the restrictions in strict mode have little effect on us. Just like a day when the Ministry of Foreign Affairs issued a notice, my country no longer issued a visa to Jamaica, although Jamaica has heard, but most people in this life may not go back, so, it doesn't matter. In the same way, Eval and with are a matter of indifference.
But even if I did not go to Jamaica, it would not prevent me from knowing that Jamaica was one of the island States of the Caribbean, a member of the Commonwealth, which was originally a British colony and was later independent. Its flag above is an X
Analogy to Eval and with
Above
The despised Eval and with