Home >Web Front-end >JS Tutorial >js code to create objects using object literals_javascript skills

js code to create objects using object literals_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:09:051178browse

Object literals provide another way to create new objects. Object literals allow object description literals to be embedded in JavaScript code, just as text data can be embedded in JavaScript code as quoted strings. An object literal consists of a list of property specifications enclosed in curly braces, separated by commas. Each property specification list in an object literal consists of a property name followed by a colon and the property value. The general format for creating objects using object literals is as follows:
var myobject = {attribute name 1: attribute value 1, attribute 2: attribute value,..., attribute name n: attribute value n}
From this definition As can be seen from the figure, this definition method actually declares a type of variable and assigns a value at the same time. Therefore, the declared object literal can be used directly in the code without having to use the new keyword to create the object.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

This method only creates An instance of this object. If you want to create multiple instances of this object, you need to directly write the object multiple times.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn