In Javascript There are several ways to make a string as a section of the JS code to execute:
- Use eval (" string to execute ")
- Use the new Function (" string to execute ")
- SetTimeOut (" string to execute ", +)
- SetInterval (" string to execute ", +)
Of course, according to Javascript best practices, we do not recommend any of the above, and of course eval () must be used when working with JSON strings, but best practices prohibit the use of new function constructors to create functions, and to disallow The passing of strings to the SetTimeout () and SetInterval () methods, the function name should be passed.
One, The difference between eval and New Function ():
Console.log ("execute function");
var val = new Function ("Console.log (' Houquan ')");
Console.log (typeof Val);
Console.log (Val);
Val ();//If not called, then it will never output "Houquan"
/**
The result returned through the new function ("...") is a function named Anonymous, which is the string that is passed to the new function () .
the central defender of an onymous means: anonymous
*/
val = new function (function () {console.log (' Test ');}) ()");
Console.log (typeof Val);
Console.log (Val);
Val ();
New Function ("Alert (' Test1 ')");
Eval ("Alert (' Test2 ')");
/**
The difference between New Function () and Eval () is that the former encapsulates the passed-in string as a Function object
Statement is returned until the returned function is called, and the action of the string is executed;
Executes the string directly as a JS code, and the returned result is an object.
*/
Console.log ("Perform eval");
val = eval ("Console.log (' houding ')");
Console.log (typeof Val);
Console.log (Val);
/**
Eval execution results under fire Fox is an object that does not return results in IE8
The structure of this object in FireFox is as follows:
*/
Execution Result:
results of the implementation under FireFox
execution results under IE8:
Second, the use of eval precautions:
Console.log ("First execution");
try{
Eval ("function () {console.log (' Error ');}");
}catch (e) {
Console.log ("Error1");
Console.log (E.tostring ());
}
/** The result of the above code execution is similar to defining an anonymous function in a global scope,
Decisive error, this is equivalent to the Global Object window, add an anonymous property, or method one
Kind, obviously can't do it!
*/
Console.log ("second Execution");
try{
Eval ("(function () {Console.log (' right ')}) ');
}catch (e) {
Console.log ("Error2");
Console.log (E.tostring ());
}
/**
The code above is equivalent to executing an immediate function like
*/
Console.log ("third Execution");
try{
Eval ("function test () {console.log (' Test ');}");
Test ();
}catch (e) {
Console.log ("Error3");
Console.log (E.tostring ());
}
/**
The above code is equivalent to defining a function
*/
Execution Result:
Eval Execution Creation object literal considerations
Console.log ("Error creating object literal case");
try{
Eval ("{name: ' Houquan '}");
}catch (e) {
Console.log ("error");
Console.log (E.tostring ());
}
/**
The code above is equivalent to the following code
*/
Name: "Houquan";
Console.log ("Correct creation of object literals");
try{
Eval ("({name: ' Houquna '})");
}catch (e) {
Console.log ("Error0");
Console.log (E.tostring ());
}
/**
In the first paragraph of the code, the JavaScript engine executes the class code block in {}, so it does not return
Expected object
The creation statement of the object literal should be enclosed in () and executed as an expression, so
To get the right results.
*/
Execution Result:
performed separately
try{
Eval ("{name: ' Houquan '}");
}catch (e) {
Console.log ("error");
Console.log (E.tostring ());
}
Execution Result:
javascript--to execute the string as code