Objects in javascript

In javascript almost everything is object.

Booleans is always object (If created with new)

var ob1 = new Boolean(true);

console.log('Type of ob1 is '+(typeof ob1))

/*

Output

"Type of ob1 is object"

*/

 

Numbers is always object (If created with new)

Strings is always object (If created with the new)

 

Notes :

Dates are always object

Maths is always object

Arrays are always object

var ob1 = [11,2,33,44];

console.log('Type of ob1 is '+(typeof ob1))

/*

Output

"Type of ob1 is object"

*/

 

Function is object

var ob1 = function(){};

console.log('Type of ob1 is '+(typeof ob1))

/*

Output

"Type of ob1 is function"

*/

 

Note : Here type of function is showing as function , but internally it is treated as an object only

Primitive type in javascript

number

string

boolean

undefined

null

 

Different ways of creating objects in javascript

a)Using the object literal

var emp = {name : ‘Mohan’,age : 21,sal : 125000}

 

b)Using new keyword

var emp = new Object();

emp.name = ‘Mohan’;

emp.age = 21;

emp.sal = 125000;

 

c)Using Object.create() : Introduced in ECMA5

5)Javascript object are mutable

They are addressed by reference and not by alue

var y = emp

Both y and emp is referring to the same object

Note : y is not a copy but the object reference itself

Any change in y will also change the object

var emp= {

 name : 'Mohan',

 age : 21,

 sal : 120000

}

var x = emp;

x.contact = 948390859034;

console.log(emp)

 

Note : Js values are not mutable , only objects

Object properties

properties of object can be added , changed, deleted

We can access the object property by following ways :

a).(dot) operator  → object.property

var emp= {

 name : 'Mohan',

 age : 21,

 sal : 120000

}

 

console.log(emp.name)

b)object[“property”]

console.log(emp[“name”])

2)Object methods

Methods are mainly meant for the objects

this opearatorrefers to the current object reference

1)

var emp= {

 firstName : 'Mohan',

 lastName : 'Kumar',

 age : 21,

 sal : 120000,

 fullName : function(){

   return this.firstName + " " + this.lastName;

 }

}

console.log(emp.fullName())

 

2)Readymade methods

a)

var name = ‘mohan’;

console.log(‘The name is ‘+name.toUpperCase())

Object constructors :

function Employee(){

 this.name = ‘Mohan kumar’;

 this.age = 21;

 this.sal = 125000;

}

//When above function  is called as Employee() then it is normal function

//When it is called with the help of new operator then it acts as an constructor

var ob = new Employee();

console.log(ob)

Let us take another example :

function Book(type,name,author){

 this.type = type;

   this.name = name;

 this.author = author

}

var ob1 = new Book('Spiritual','Bhagwat Gita','Mohan kumar');

var ob2 = new Book('Technical','Let Us C','Yashwant Kanetkar');

//Now let us add a new feature cost to ob2

ob2.cost = 135;

console.log(ob2)

/*

Output :

[object Object] {

 author: "Yashwant Kanetkar",

 cost: 135,

 name: "Let Us C",

 type: "Technical"

}



*/

 

//Now let us print the ob1, because we have a new property to the ob2 object

console.log(ob1)

/*

Output :

[object Object] {

 author: "Mohan kumar",

 name: "Bhagwat Gita",

 type: "Spiritual"

}



*/

 

 

//Note no property gets added to the ob1 object

 

//It means the main constructor structure not changed

 

//We can do this through thr prototype property of object

 

We can not add new property to the object constructor as we add to the existing object

 

We can add new property to the existing object constructor by 2 ways :

i)By declaring it inside the constructor

function Book(type,name,author,cost){

 this.type = type;

 this.name = name;

 this.author = author;

 this.cost = cost;

}

 

var ob1 = new Book(‘Spiritual’,’Bhagwat Gita’,’Mohan kumar’,95);

var ob2 = new Book(‘Technical’,’Let Us C’,’Yashwant Kanetkar’,145);

console.log(ob1,ob2)

/*

Output :

 

[object Object] {

 author: “Mohan kumar”,

 cost: 95,

 name: “Bhagwat Gita”,

 type: “Spiritual”

}

[object Object] {

 author: “Yashwant Kanetkar”,

 cost: 145,

 name: “Let Us C”,

 type: “Technical”

}

 

*/

 

ii)By using prototype model

 

In javascript the object inherit property from it’s prototype

Array object inherit property from the Array.prototype

Date object inherit property from Date.prototype

Employee object will inherit from Employee.prototype

 

function Book(type,name,author,cost){

 this.type = type;

 this.name = name;

 this.author = author;

}

 

Book.prototype.cost = 99;

 

var ob1 = new Book(‘Spiritual’,’Bhagwat Gita’,’Mohan kumar’);

var ob2 = new Book(‘Technical’,’Let Us C’,’Yashwant Kanetkar’);

console.log(ob1,ob2)

 

/*

Output

  • [object Object] {
  •  author: “Mohan kumar”,
  •  cost: 99,
  •  name: “Bhagwat Gita”,
  •  type: “Spiritual”
  • }
  • [object Object] {
  •  author: “Yashwant Kanetkar”,
  •  cost: 99,
  •  name: “Let Us C”,
  •  type: “Technical”
  • }

 

*/

 

Again we can add the method to the existing object constructor

function Book(type,name,author,cost){

 this.type = type;

 this.name = name;

 this.author = author;

}

 

Book.prototype.cost = 99;

 

Book.prototype.getDetails = function(){

 return this.name+” written by “+this.author+” with cost “+this.cost

}

 

var ob1 = new Book(‘Spiritual’,’Bhagwat Gita’,’Mohan kumar’);

console.log(ob1.getDetails())

/*

Output

“Bhagwat Gita written by Mohan kumar with cost 99”

*/

By Pankaj Kumar Agarwal

Leave a reply:

Site Footer