Which Are The Object Properties Used In Java Script

admin_img Posted By Bajarangi soft , Posted On 10-10-2020

Properties are the most important part of any JavaScript object. so today we discuss about object properties

Which Are The Object Properties Used In Java Script

JavaScript Properties

Properties are the values associated with a JavaScript object.

A JavaScript object is a collection of unordered properties.

Properties can usually be changed, added, and deleted, but some are read only.


Accessing JavaScript Properties

The syntax for accessing the property of an object is:

objectName.property         // person.age
or

objectName["property"]      // person["age"]
or

objectName[expression]      // x = "age"; person[x]

The expression must evaluate to a property name.


Example (1)

document.getElementById("demo1").innerHTML = person.firstname + " is " + person.age + " years old.";//example 1
 

Example (2)

JavaScript for...in Loop

The JavaScript for...in statement loops through the properties of an object.

Syntax

for (variable in object) {
    // code to be executed
}

The block of code inside of the for...in loop will be executed once for each property.

Looping through the properties of an object:

Example(3)

var txt = "";
var person = {fname:"John", lname:"Doe", age:25};
var x;
for (x in person) {
    txt += person[x] + " ";
}
document.getElementById("demo").innerHTML = txt;


Adding New Properties

You can add new properties to an existing object by simply giving it a value.

Assume that the person object already exists - you can then give it new properties:

Example(4)

var person = {
    firstname:"John",
    lastname:"Doe",
    age:50,
    eyecolor:"blue"
};

person.nationality = "English";
document.getElementById("demo").innerHTML =
    person.firstname + " is " + person.nationality + ".";


Deleting Properties

The delete keyword deletes a property from an object:

var person = {
    firstname:"John",
    lastname:"Doe",
    age:50,
    eyecolor:"blue"
};

delete person.age;
document.getElementById("demo").innerHTML =
    person.firstname + " is " + person.age + " years old.";
 

The delete keyword deletes both the value of the property and the property itself.

After deletion, the property cannot be used before it is added back again.

The delete operator is designed to be used on object properties. It has no effect on variables or functions.

The delete operator should not be used on predefined JavaScript object properties. It can crash your application.


Property Attributes

All properties have a name. In addition they also have a value.

The value is one of the property's attributes.

Other attributes are: enumerable, configurable, and writable.

These attributes define how the property can be accessed (is it readable?, is it writable?)

In JavaScript, all attributes can be read, but only the value attribute can be changed (and only if the property is writable).

( ECMAScript 5 has methods for both getting and setting all property attributes)


Prototype Properties

JavaScript objects inherit the properties of their prototype.

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.

Complete Code For Object Properties Used In Java Script

<!DOCTYPE html>
<html>
<head>
    <title>Which Are The Object Properties Used In Java Script</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
    body{
        background: black;
    }
</style>
<body>
<div class="container">
    <br>
    <div class="text-center">
        <h1 id="color" style="color: White">Which Are The Object Properties Used In Java Script</h1>
    </div>

    <div class="well">
        <h2 id="demo1"></h2>
        <h2 id="demo2"></h2>
        <h2 id="demo3"></h2>

        <script>
            var person = {
                firstname:"John",
                lastname:"Doe",
                age:50,
                eyecolor:"blue"
            };

            document.getElementById("demo1").innerHTML = person.firstname + " is " + person.age + " years old.";//example 1
            document.getElementById("demo2").innerHTML = person["firstname"] + " is " + person["age"] + " years old.";//example 2

            //example 3
            var txt = "";

            var x;
            for (x in person) {
                txt += person[x] + " ";
            }
            document.getElementById("demo3").innerHTML = txt;
            //example 4
            person.nationality = "English";
            document.getElementById("demo4").innerHTML =
                person.firstname + " is " + person.nationality + ".";
            // example 5
            delete person.age;
            document.getElementById("demo5").innerHTML =
                person.firstname + " is " + person.age + " years old.";
        </script>
    </div>
</div>
</body>
</html>

 

Related Post