Welcome
Login / Register

Object literal vs object constructor

Featured video
You need to have the Flash Player installed and a browser with JavaScript support.

Thanks! Share it with your friends!

URL

You disliked this video. Thanks for the feedback!

URL


Published by Admin in Java Script
487 Views

Description

Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2015/02/object-literal-vs-object-constructor.html

In this video we will discuss the main difference between objects created using object literal and constructor function and when to use one over the other. In Part 53 of JavaScript tutorial we discussed some of the syntactical differences.

Creating an object using object literal notation

[script type="text/javascript"]
var employee =
{
name : "John"
}

// Create a new variable and assign the employee object
var newEmployee = employee;

// Change the name property of the employee object using the new variable
newEmployee.name = "Mary";

// Retrieve the name property from the original employee object
// Notice that name is changed to Mary
document.write(employee.name);
[/script]

Output : Mary

Objects created using object literals are singletons. This means when a change is made to the object, it affects that object across the entire script.

Creating an object using constructor function

[script type="text/javascript"]
var emp = function ()
{
this.name = "John";
}

// Create an instance of employee
// employee.name will return John
var employee = new emp();

// Create an other instance of employee
// newEmployee.name will return John
var newEmployee = new emp();

// Change the name property of the newEmployee object
newEmployee.name = "Mary";

// Retrieve the name property from the original employee object
// Notice that name is not changed to Mary, it is still John
document.write(employee.name);
[/script]

Output : John

Object defined with a function constructor lets you have multiple instances of that object. This means change made to one instance, will not affect other instances.

So, when to use one over the other?
If you need multiple instances of the object use constructor function where as if you need just one instance of the object then use literal notation.

Show more

Post your comment

Comments

Be the first to comment