Home > Article > Web Front-end > A brief discussion on the function and scope of eval()
The role of eval()
Parses string parameters into js code and runs it, and returns the execution result;
The scope of eval
The scope is valid in all its scope contents
IE8 and below point to window
Solution:
functiona(){
if(window.execScript){ // Supports IE8 and below versions
window.execScript("var x=1");
}
else{ //Commonly used browsers support
"var x=1 ");
Notes
You should avoid using eval, which is unsafe and very performance-consuming (twice, once parsed into a js statement and once executed).The above is the detailed content of A brief discussion on the function and scope of eval(). For more information, please follow other related articles on the PHP Chinese website!