javascript--to execute the string as code

Source: Internet
Author: User

In Javascript There are several ways to make a string as a section of the JS code to execute:

    1. Use eval (" string to execute ")
    2. Use the new Function (" string to execute ")
    3. SetTimeOut (" string to execute ", +)
    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.