Home  >  Article  >  Web Front-end  >  Detailed explanation of the definition, usage and side effects of JavaScript global function eval with examples

Detailed explanation of the definition, usage and side effects of JavaScript global function eval with examples

伊谢尔伦
伊谢尔伦Original
2017-07-25 14:00:182608browse

eval() is a global function, javascript uses eval() to interpret and run a string 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 parameter passed in is not a string, it returns this parameter directly. If the parameter is a string, it will compile the string as javascript code. If compilation fails, a syntaxError exception is thrown. If the compilation is successful, execution of this code begins and the value of the last expression or statement in the string is returned. If the last expression or statement has no value, undefined is finally returned. If 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 values ​​of variables and defines new variables and functions exactly the same as 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 through an alias, eval() will execute its string as top-level global code. The executed code may define new global variables and global functions, or assign values ​​to global variables, but it cannot use or modify local variables in the function


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-The result of calling eval() through an alias in the browser is the same as calling eval() normally

Side effects

The javascript interpreter has done a lot Code analysis and optimization. The problem with eval() is that the code used for dynamic execution usually cannot be analyzed, so the interpreter cannot optimize it, which will lead to performance degradation

Similar to eval() is setTimeout (), setInterval(), new Function(), etc. These functions can take strings as parameters and be executed dynamically when the program is running. The benefits of this execution mechanism cannot offset its performance loss, so you should try to avoid using

strict mode

Due to eval( ) function is too powerful, strict mode imposes strict restrictions on it

[1]Variables or functions cannot be created through the eval() function, but their values ​​can be queried and changed


'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】It is forbidden to use eval as an identifier


'use strict';
var eval = 10;//SyntaxError: Unexpected eval or arguments in strict mode

The above is the detailed content of Detailed explanation of the definition, usage and side effects of JavaScript global function eval with examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn