JavaScript to create objects with literal content _javascript tips

Source: Internet
Author: User

In JavaScript, you can create a new object directly by literal volume:


Copy Code code as follows:

var obj = {a:27, "B": 99};


As noted above, when you create an object in literal quantities, the property definition in the object can be included either in single or double quotes or by ignoring the quotation marks. However, when the property appears in spaces, slashes and other special characters, or use the property and JS keyword conflict, you must use quotation marks.

When you create an object in literal quantities, the property can be an empty string, and a space can appear in the property:


Copy Code code as follows:

Empty string is allowed as Object
var o = {"": the "P": 99};
Console.log (o);//object {=88, p=99}

Spaces can be included into property
var O2 = {"Good score": "," Bad Score ": 52};
Console.log (O2);//object {good score=99, bad score=52}


It's worth noting that, even if you use the same literal, JavaScript creates an entirely new object each time you use a literal:


Copy Code code as follows:

Every object literal creates a new and distinct object.
var x = {a:18, b:28};
var y = {a:18, b:28};
Console.log (x = = y);//false


In literal quantities, if the last comma ("}" character appears before ","), then some JavaScript interpreters will complain. In fact, in IE7, this behavior will cause the browser to suspend animation and other problems. In the ECMAScript 5 standard, "," appears before "," is legal, the comma will be directly ignored.

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.