filmov
tv
Object literal vs object constructor

Показать описание
Link for all dot net and sql server video tutorial playlists
Link for slides, code samples and text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
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
// Retrieve the name property from the original employee object
// Notice that name is changed to Mary
[/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 ()
{
}
// Create an instance of employee
var employee = new emp();
// Create an other instance of employee
var newEmployee = new emp();
// Change the name property of the newEmployee object
// Retrieve the name property from the original employee object
// Notice that name is not changed to Mary, it is still John
[/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.
Link for slides, code samples and text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
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
// Retrieve the name property from the original employee object
// Notice that name is changed to Mary
[/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 ()
{
}
// Create an instance of employee
var employee = new emp();
// Create an other instance of employee
var newEmployee = new emp();
// Change the name property of the newEmployee object
// Retrieve the name property from the original employee object
// Notice that name is not changed to Mary, it is still John
[/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.
Комментарии