Why the JavaScript eval function parses JSON data with parentheses eval ("(" +data+ ")") __ Data structure

Source: Internet
Author: User
Tags opening and closing tags javascript eval
Why the JavaScript eval function, when parsing JSON data, is prefixed with parentheses.
Why do I have to eval here to add "(" ("+data+"));



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

The purpose of parentheses is to force the Eval function to force an expression in parentheses (expression) into an object when processing JavaScript code instead of executing as a statement (statement). For example, an object literal {}, if the outer bracket is not added, Eval recognizes the curly braces as the opening and closing tags of the JavaScript code block, then {} will be considered to have executed an empty statement.



*************************************************************************************************************** *************************************************************************

"JavaScript Advanced Programming second Edition" Page73, fifth chapter, reference type

5.1object type

So far, most of the reference type values we see are instances of type object, and object is also the most used type in ECMAScript. Although an instance of object does not have much functionality, it is really an ideal choice for storing and transferring data in an application.

There are two ways to create an instance of an object. The first is to use the new operator followed by the object constructor, as follows:

Var person=new object ();

Person.name= "Nicholas";

person.age=29;

Another way is to use an object literal notation. Object literals are a shorthand form of object definitions to simplify the process of creating objects that contain a large number of properties. The following example uses the object literal syntax to define the same person object as in the previous example:

var person={

Name: "Nicholas",

Age:29

};

In this example, the left curly brace ({) Represents the beginning of the literal of the object, because it appears after the assignment operator (in other environments, the left curly braces represent the beginning of the statement block). We then define the Name property, followed by a colon, followed by the value of this property. In object literals, commas are used to separate different properties, so "Nicholas" is followed by a comma. However, you cannot add a comma after the value 29 of the Age property, because the is the last property of the object. Adding a comma after the last attribute causes an error in IE and opera.

Property names can also use strings when using object literal syntax, as shown in the following example:

var person={

"Name": "Nicholas",

"Age": 29

};

The above code will also get the same result as the previous example, creating a new object with the name and age attributes.

In addition, if you leave the curly braces blank when you use object literal syntax, you can define objects that contain only default properties and methods.

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.