filmov
tv
Prototype in JavaScript

Показать описание
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 Prototype object in JavaScript. Let us understand it's use with an example.
The following Employee constructor function constructs Employee object.
function Employee(name)
{
}
There are several ways to add a function to the Employee object. One way is as shown below. This works but the problem with this approach is that, if you create 100 employee objects there will be 100 copies of getName() function. We don't want to be creating copies of functions, instead we want all the objects to share the same function code. This can be achieved using JavaScript prototype object.
function Employee(name)
{
{
}
}
var e1 = new Employee("Mark");
var e2 = new Employee("Sara");
Output :
function Employee(name)
{
}
var e1 = new Employee("Mark");
{
}
var e2 = new Employee("Sara");
In the following example getName() function is added to the Employee object using the name of the constructor function. This would also result in undefined error.
function Employee(name)
{
}
{
}
var e1 = new Employee("Mark");
var e2 = new Employee("Sara");
Using the prototype object to add functions : The following are the advantages of using the prototype object to add functions.
1. No matter how many objects you create, functions are loaded only once into memory
2. Allows you to override functions if required.
function Employee(name)
{
}
{
}
var e1 = new Employee("Mark");
var e2 = new Employee("Sara");
Output :
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 Prototype object in JavaScript. Let us understand it's use with an example.
The following Employee constructor function constructs Employee object.
function Employee(name)
{
}
There are several ways to add a function to the Employee object. One way is as shown below. This works but the problem with this approach is that, if you create 100 employee objects there will be 100 copies of getName() function. We don't want to be creating copies of functions, instead we want all the objects to share the same function code. This can be achieved using JavaScript prototype object.
function Employee(name)
{
{
}
}
var e1 = new Employee("Mark");
var e2 = new Employee("Sara");
Output :
function Employee(name)
{
}
var e1 = new Employee("Mark");
{
}
var e2 = new Employee("Sara");
In the following example getName() function is added to the Employee object using the name of the constructor function. This would also result in undefined error.
function Employee(name)
{
}
{
}
var e1 = new Employee("Mark");
var e2 = new Employee("Sara");
Using the prototype object to add functions : The following are the advantages of using the prototype object to add functions.
1. No matter how many objects you create, functions are loaded only once into memory
2. Allows you to override functions if required.
function Employee(name)
{
}
{
}
var e1 = new Employee("Mark");
var e2 = new Employee("Sara");
Output :
Комментарии