×

Html & Html5

it training classes Logo

The World's best Software Development Study Portal

Js Objects


Our Javascript is an object based language. Here we direct create the object to solve out our entity. Everything near us can be the objects like chair, pen, Comb, etc. We have some ways to create the objects which are described as follows:-:-

  • By object literal
  • creating instance of object by using new keyword
  • By using object constructor

Creating object by object literal

Syntax

object={property1 : value1 , property2 : value2...propertyN : valueN}

Let's take an example:-

<html>

<body>

<script>

   person ={name:"Manish" , class:12}

   document.write(person.name+ " " +person.class);

}

</script>

</body>

</html>

Output of this program

Manish 12

This is one way how we create the object.



Creating instance of Object

We use the new keyword to create an object

Syntax

var object = new Object();

Let's take an example:-

<html>

<body>

<script>

   var person =new Object();

   person.name="Manish";

   person.class=12;

   document.write(person.name+ " " +person.class);

}

</script>

</body>

</html>

Output of this program

Manish 12

This is other way how we create the object.



Using the object constructor

In this way first we create any function with arguments and we can also use this keyword here which refers to the current object.

Let's take an example:-

<html>

<body>

<script>

   function person(name , class){

   this.name = name

   this.class =class

}

   per = new person{"Manish" , 12}

   document.write(person.name+ " " +person.class);

}

</script>

</body>

</html>

Output of this program

Manish 12

This is third way how we create the object.



Some more methods and description below :-

Methods Description
Object.assign() This method is used to copy enumerable and own properties from a source object to a target object
Object.create() This method is used to create a new object with the specified prototype object and properties.
Object.defineProperty() This method is used to describe some behavioral attributes of the property.
Object.is() This method determines whether two values are the same value.
Object.isExtensible() This method determines if an object is extensible
Object.isFrozen() This method determines if an object was frozen.
Object.isSealed() This method determines if an object is sealed.
Object.keys() This method returns an array of a given object's own property names.
Object.preventExtensions() This method is used to prevent any extensions of an object.
Object.seal() This method prevents new properties from being added and marks all existing properties as non-configurable.
Object.setPrototypeOf() This method sets the prototype of a specified object to another object.
Object.values() This method returns an array of values.



JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute