Thursday 30 August 2018

JavaScript: Objects

An object is an entity that is uniquely identified among other objects. In JavaScript, object is modeled as unordered collection of properties, each property has a name and value associated with it.

var obj = {property1: value1, property2: value2, ....propertyN: valueN};

In the following post, I explained about an object in general.

How to define object?
Following three ways are used to define an object.

var obj = new Object(); // Define empty object
var obj = {}; // Define empty object
var obj = {property1: value1, property2: value2, ....propertyN: valueN}; // Define object with properties.

How to add properties to an object?
You can add properties to an object using . notation (or) by using [].

Syntax
object.property = value;
object["property"] = value;

How to access the value associated with an object?
It is same like adding properties to an object. You can access the value associated with an object using . (or) [] notation.

Syntax
var a = object.property; // Access the value associated with given property.

var a = object["property"]; // Access the value associated with given property.

object.html
<!DOCTYPE html>

<html>

<head>
    <title>Objects example</title>
</head>

<body>
    <script type="text/javascript">
        var person1 = new Object();
        var person2 = {};
        var person3 = {
            firstName: "Hari Krishna",
            lastName: "Gurram",
            age: 27
        }

        // Adding properties to person1
        person1.firstName = "Senthil";
        person1.organization = "Symantec";

        // Adding properties to person2
        person2["firstName"] = "Sandesh";
        person2["id"] = 54321;

        /* Accessing using . notation */
        document.write("person1 details <br />");
        document.write("******************************************<br/>");
        document.write("firstName : " + person1.firstName + "<br />");
        document.write("lastName : " + person1.lastName + "<br />");
        document.write("age : " + person1.age + "<br />");
        document.write("organization : " + person1.organization + "<br />");
        document.write("id : " + person1.id + "<br />");

        /* Accessing using [] */
        document.write("<br />person2 details <br />");
        document.write("******************************************<br/>");
        document.write("firstName : " + person2["firstName"] + "<br />");
        document.write("lastName : " + person2["lastName"] + "<br />");
        document.write("age : " + person2["age"] + "<br />");
        document.write("organization : " + person2["organization"] + "<br />");
        document.write("id : " + person2["id"] + "<br />");

        /* Accessing using . notation */
        document.write("<br />person3 details <br />");
        document.write("******************************************<br/>");
        document.write("firstName : " + person3.firstName + "<br />");
        document.write("lastName : " + person3.lastName + "<br />");
        document.write("age : " + person3.age + "<br />");
        document.write("organization : " + person3.organization + "<br />");
        document.write("id : " + person3.id + "<br />");
    </script>
</body>

</html>

Objects in JavaScript are dynamic, you can add properties to an object any time and delete properties any time.



Previous                                                 Next                                                 Home

No comments:

Post a Comment