The connection between JavaScript eval and JSON _javascript tips

Source: Internet
Author: User
Tags javascript eval
If you want to learn more about EV For Al and JSON, refer to the following links:

EVal:https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Ev Al

JSON:http://www.json.org/

Howthe Ev al function works

TheEv al function evaluates a given string containing Javascript code, and attempts to execute an expression contained in a string or a series of legitimate Javascript statements. TheEv al function takes the last expression or the value or reference contained in the statement as the return value.

Give an example to explain

    • EV-Al evaluationjavas cript expression
' Bar ' ;
Eval(' foo ' + Bar ');
Alert(Foobar);
    • EVal evaluation Javascript statement
VarBar= ' Bar ';
If variable bar equals ' bar ', foobar is the result of
Last executing statement:bar= "Foo-bar";
VarFoobar= Eval(' if (bar = = ' Bar ') {bar= ' Foo-bar ';} else {bar = ' bar-foo ';} ');
Alert(Foobar); Change the Valuebar = ' foo ';
Now we the last executed statement is:bar = "Bar-foo";
Therefore the value of variable foobar has been changed
Into ' Bar-foo '
Foobar = eval(' if (bar = = ' Bar ') {bar= ' Foo-bar ';} else {bar = ' bar-foo ';} ' );
Alert(Foobar);

Format of JSON

The format of JSON is made up of curly braces and a name-value pair consisting of a colon (:). Note the difference between the JSON format and the object literal: The literals of the JSON name is strictly expressed in quotes + names.

Give an example to explain

    • The literal amount of the object
VarObjectliteral= {
name Age Special: Sayname: function {
return . }
}
    • JSON object
VarJsonformat= {
"Summary": "Blogs",
"Blogrolls": [
{
"title": } {
Span class= "Sh_symbol" >: }
Span class= "Sh_symbol" >]
}

EVAl and JSON

Thanks to the rise of Ajax, the lightweight data format of JSON became increasingly popular as a transfer format between the client and the server, and the problem was how to convert the server-side-built JSON data intousable Javas cript objects. Using theev al function is a simple and direct method. When converting, you need to wrap the outside of the JSON string in parentheses:

Eval("("));   

Why do I have braces?

The purpose of parentheses is to forcethe Ev al function toforce an expression in parentheses (expression) into an object when evaluating Javas cript code, Instead of executing as a statement (statement). For example, an object literal {}, if the outer bracket is not added, Eval will recognize the curly braces as the opening andclosing tags of the Javas cript code block, then {} will be considered to have executed an empty statement. So the following two execution results are different:

Alert(eval("{}"//return undefined
Alert(eval({})); return Object[object]

What is the name part of the JSON format to quote?

Because theev al function interprets {foo: "Bar"} as a legitimate Javascript statement, not an expression. But what people often want is for EVal to interpret this code as an object. So the JSON format forces you to add quotes and parentheses around the outside of the name so that EVAl doesn't incorrectly interpret JSON as a block of code.

Give an example to explain

    • EVAl Error Resolution semantics
Alert(eval(' {foo: ' Bar '} '//Return ' bar '), incorrect    
    • EVal correctly parses JSON
Alert(eval({"foo": "Bar"}) '//Return JSON object, correct   

Conclusion

Understand howev al Works and JSON's strict qualifying format, combining EVAl and json to Javascript Data Delivery and object conversion.

Following this format:

Eval(' {') ');   
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.