Home>

Previous words

Eval and with are often rejected,It seems that their existence is a mistake.In css, the form is rejected,Just use tables to display data in web pages,Instead of doing layout,May be dismissed as irregular,Overkill.What about eval and with?This article will detail the eval () function and the with statement

eval

definition

Eval () is a global function,javascript eval () to interpret and run strings composed of javascript source code

var result=eval ("3 + 2");
console.log (result, typeof result);//5 "number"

usage

Eval () has only one parameter,If the argument passed is not a string,It returns this parameter directly.If the argument is a string,It compiles strings as javascript code.If compilation fails, a syntaxerror exception is thrown. If the compilation is successful,Then start executing this code,And returns the value of the last expression or statement in the string,If the last expression or statement has no value,It will eventually return undefined. If the string throws an exception,This exception will pass the call to eval ()

var num=1;
var str="test";
console.log (eval (num));//1
console.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 in which it is called.That is,It looks up the value of a variable and defines new variables and functions in exactly the same way as code in local scope

var b=2;
function foo (str, a) {
eval (str);
console.log (a, b);
}
foo ("var b=3;", 1);//1 3

Alias

transfer When called via an alias,eval () executes its string as the top-level global code.The 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

var geval=eval;
var x="global", y="global";
function f () {
var x="local";
eval ("x + =" changed ";");
return x;
}
function g () {
var y="local";
geval ("y + =" changed ";");
return y;
}
console.log (f (), x);//localchanged global
console.log (g (), y);//local globalchanged

[Note] ie8-Browser calls eval () via alias and the result of eval ()

side effect

JavaScript interpreter does a lot of code analysis and optimization.The problem with eval () is thatThe code used for dynamic execution cannot usually be analyzed,So the interpreter cannot optimize it,This will cause performance degradation

Similar to eval () are settimeout (), setinterval (), new function (), etc. These functions can take strings as parameters.Dynamically executed while the program is running.The benefits of this execution mechanism cannot offset the loss in performance,So you should try to avoid it.

Strict mode

Because eval () function is too powerful,Strict mode places strict restrictions on it

[1] Variables or functions cannot be created through the eval () function,But you can query and change its value

"use strict";
eval ("var x=1;");
console.log (x);//referenceerror:x is not defined
"use strict";
var x=1;
eval ("x=2;");
console.log (x);//2

[2] Prohibit 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 mainly to simplify the work of writing the same object multiple times

With statement adds object to the head of the scope chain,Then execute the statement, and finally restore the scope chain to the original state

with (object) {
Statement;
}

effect

WithWith deep object nesting levels, the with statement is usually used to simplify code writing.Instead, it basically handles a reference to an object as a scope.Treat the properties of the object as identifiers in scope,Thus creating a new lexical scope

Client In client-side JavaScript, you may use expressions like the following to access elements in an html form

document.forms [0] .address.value

If this expression occurs multiple times in the code,You can use the with statement to add the form object to the top of the scope chain

with (document.forms [0]) {
name.value="";
address.value="";
emai.value="";
}

This method reduces a lot of input,No more document.forms [0] prefix for each property name. This object is temporarily mounted on the scope chain,When JavaScript needs to parse identifiers such as address,Will automatically find in this object

note [Note] The with statement provides a shortcut to read the properties of an object,But it does not create the properties of the object

If object o has a property 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 the attribute x is not defined in o, the following code is exactly the same as the code x=1 without the with statement.This is because lhs query is performed on the variable x and 1 is assigned to it

var o=();
with (o) x=1;
console.log (o.x);//undefined
console.log (x);//1

side effect

Similar to eval, the JavaScript code of the with statement is very difficult to optimize.It also makes it difficult to debug code,And compared to code without a with statement,It runs slower

Also, if the with statement is inappropriate,It may also cause variable leakage,Pollution of global scope

var x=1;
var o=();
with (o) {
x=2;
}
console.log (x);//2
console.log (o.x);//undefined

Strict mode

In strict mode,Prohibit the use of the with statement

//syntaxerror:strict mode code may not include a with statement
"use strict";
var o=();
with (o) {
x=2;
}

At last

The use of eval and with makes the engine unable to optimize the scope lookup at compile time,Leading to performance degradation,Code runs slower.Because eval and with are rarely used in actual work,So the restrictions in strict mode,It has little effect on us.It ’s like, for example, the Ministry of Foreign Affairs issued an announcement one day,China no longer issues visas to Jamaica,Although Jamaica has heard it,But most people may not go there once in their lifetime,So it doesn't matter.Similarly, eval and with are rejected and not rejected,It doesn't matter

  • Previous JAVA immutable mechanism and immutability of String (recommended)
  • Next Steps to install mysql server in CentOS 7