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

About Author

Author Image
Arun Kumar

Arun is a creative UI Developer

Request for Proposal

Name is required

Comment is required

Sending message..