The Eval method in JS

Source: Internet
Author: User

Eval (String)

The eval function receives a parameter string, and if string is not a string, the string is returned directly. Otherwise, a string statement is executed. Returns this value if the string statement execution result is a value, otherwise returns undefined.
It is important to note that the object declaration syntax "{}" does not return a value and needs to be enclosed in parentheses to return a value, a simple example is as follows:

As you can see, for an object declaration statement, it is only execution and cannot return a value. In order to return an object declaration statement such as a commonly used "{}", it must be enclosed in parentheses to convert it to an expression in order to return its value. This is one of the basic principles of AJAX development using JSON. As you can see clearly in the example, the second alert statement outputs undefined, and the third one with parentheses outputs the object represented by the statement.

Eval parsing JSON

Using jquery to parse JSON data as a transport object for jquery asynchronous requests, the result of the jquery request is the JSON object, which is all about the server returning the form of a string in JSON form. For JSON objects that are encapsulated with plug-ins such as Jsonobject, this is also the same thing, and is no longer explained here.
Here we first give a set of JSON strings, the string set as follows:

The code is as follows:
var data= "
{
Root
[
{name: ' 1 ', Value: ' 0 '},
{name: ' 6101 ', Value: ' Beijing '},
{name: ' 6102 ', Value: ' Tianjin '},
{name: ' 6103 ', Value: ' Shanghai City '},
{name: ' 6104 ', Value: ' Chongqing '},
{name: ' 6105 ', Value: ' Weinan '},
{name: ' 6106 ', Value: ' Yanan '},
{name: ' 6107 ', Value: ' Hanzhong '},
{name: ' 6108 ', Value: ' Yulin '},
{name: ' 6109 ', Value: ' Ankang '},
{name: ' 6110 ', Value: ' Shangluo '}
]
}";
This is based on the data type obtained by jquery asynchronously,--json object and string, and describes how the results are handled in two ways.

1. For the JSON string returned by the server, if the jquery asynchronous request does not make a type description, or is accepted as a string, then an object processing is required, either in a cumbersome way or by placing the string in eval (). This is also a good way to get JSON objects in a normal Javascipt way, as illustrated below:

var dataobj=eval ("(" +data+ ")");//Convert to JSON object

Why to Eval here to add "(" ("+data+");//"?

The reason is that eval itself is a problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.

The purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. So the following two execution results are different:
Alert (eval ("{}");//return undefined
Alert (eval ("({})");//return Object[object]

For this kind of writing, in JS, you can see everywhere.

such as: (function ()) {} (); Do the closure operation and so on.

The Eval method in JS

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.