Object literal pattern in javascript
Posted By : Arun Kumar | 29-Oct-2015
In javascript objects can be create with new object() function or the literal declaration
ex:var myObject = new object();
and adding properties to this newly created object can be done by:
myObject["property"] = "value";
myObject["otherproperty"] = "value";
and to ready the property
myObject["property"];
This will output the value assigned to the property named "property" from the myObject object.
Instead of creating and assigning properties and values in multiple statements.We can use literal pattern to declare the object.
Object literal pattern:
Object can be declared with curly braces.Such as myObject = {};
or an object can be declared with property itself.
such as myObject = {
property:"value",
otherproperty:"value"
}
and any of the property can be accessed using the dot operator between the object name and the property.
ex: myObject.property
We can also set a property value using the dot operator as myObject.property = "some other value".
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Arun Kumar
Arun is a creative UI Developer