Home >Web Front-end >JS Tutorial >Introduction to creating objects using literals in JavaScript_javascript tips
In JavaScript, you can use literals to directly create a new object:
As mentioned above, when using literals to create objects, the property definition in the object can be enclosed in single or double quotes, or the quotes can be ignored. However, when special characters such as spaces and slashes appear in the property, or when the property used conflicts with JS keywords, quotation marks must be used.
When using literals to create objects, the property can be an empty string, and spaces can also appear in the property:
//spaces can be included in property
var o2 = {"good score":99, "bad score":52};
console.log(o2);//Object {good score=99, bad score=52}
It’s worth noting that JavaScript creates a completely new object every time a literal is used, even if the same literal is used:
In a literal, if there is an extra comma at the end ("," appears before the "}" character), some JavaScript interpreters will report an error. In fact, in IE7, this behavior will cause the browser to freeze and other problems. In the ECMAScript 5 standard, it is legal for "," to appear before the "}" character, and the comma will be ignored.