JavaScript Objects


A javaScript object is an entity having state and behavior (properties and method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.

JavaScript is an object-based language. Everything is an object in JavaScript.

JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.


Creating Objects in JavaScript

There are 3 ways to create objects.

  • By object literal
  • By creating instance of Object directly (using new keyword)
  • By using an object constructor (using new keyword)

JavaScript Object by object literal

Syntax

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

Example

<script> 
student={id:11211855,name:"Sachin",section:"JK201"}  
document.write(student.id+" "+student.name+" "+student.section);  
</script>
Output
11211855 Sachin JK201

By creating instance of Object

Syntax

var objectname = new Object(); 

Example

<script> 
var student = new object();
student.id = 11211855;
student.name = "Sachin";
student.section = "JK201";  
document.write(student.id+" "+student.name+" "+student.section);  
</script>
Output
11211855 Sachin JK201

By using an Object constructor

Here, you need to create function with arguments. Each argument value can be assigned in the current object by using this keyword.

The this keyword refers to the current object.

Example

<script> 
function student(id,name,section)
{
student.id = 11211855;
student.name = "Sachin";
student.section = "JK201";  
document.write(student.id+" "+student.name+" "+student.section);  
}
</script>
Output
11211855 Sachin JK201

The this Keyword

In JavaScript, the thing called this, is the object that "owns" the JavaScript code.

The value of this, when used in a function, is the object that "owns" the function.

The value of this, when used in an object, is the object itself.

The this keyword in an object constructor does not have a value. It is only a substitute for the new object.

The value of this will become the new object when the constructor is used to create an object.


Defining Methods for an Object

An object by assigning methods to it.

<script type="text/javascript">

// Define a function which will work as a method
function addPrice(amount){
    this.price = amount; 
}

function book(title, author){
    this.title = title; 
    this.author  = author;
    this.addPrice = addPrice; // Assign that method as property.
}
	
   var myBook = new book("JAVASCRIPT", "Brendan Eich");
   myBook.addPrice(250);
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
   document.write("Book price is : " + myBook.price + "<br>");
</script>
Output
Book title is : JAVASCRIPT
Book author is : Brendan Eich
Book price is : 250

The with Keyword

The with keyword is used as a kind of shorthand for referencing an object's properties or methods.

Syntax

with (object){
    properties used without the object name and dot
}

Example

<script type="text/javascript">

// Define a function which will work as a method
function addPrice(amount){
    with(this){
       price = amount; 
    }
}
function book(title, author){
    this.title = title; 
    this.author  = author;
    this.price = 0;
    this.addPrice = addPrice; // Assign that method as property.
}

   var myBook = new book("JAVASCRIPT", "Brendan Eich");
   myBook.addPrice(250);
   document.write("Book title is : " + myBook.title + "<br>");
   document.write("Book author is : " + myBook.author + "<br>");
   document.write("Book price is : " + myBook.price + "<br>");
</script>
Output
Book title is : JAVASCRIPT
Book author is : Brendan Eich
Book price is : 250

Share this article on