Tech Junkie Blog: JavaScript Objects Deep Dive : Creating Objects With Object Literals

Wednesday, April 26, 2017

JavaScript Objects Deep Dive : Creating Objects With Object Literals

The most common ways you see how objects are created in JavaScript is the object literal syntax.  The object literal syntax is the open { } with comma separated name: pair value lists.  The name: part is the name of the property.  The part after : is the property value which can hold a primitive value or another object.  It could even be a function.

Here is an example of an object literal:

var product = {
    name: "Chai",
    Category: "Tea",
    Country: "India",
    supplier: {
        name: "ACME Tea Of India",
        location : "New Delhi"
    },
    related: ["Earl Grey", "Green Tea", "Dark Tea", "White Tea"],
    display: function () {
        console.log(this);
    }
};



If you open this in the browser you will see that product object is made up of properties, another object called supplier, an array related, and a function called display()


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete