The prototype property allows us to add properties and methods to an object.
Please note that we are not talking about the Prototype JavaScript Framework implemented in prototype.js
Prototype Property and Dynamic inheritance:
The functions in JavaScript are objects and prototype is one of is property.
Eg.
>>>var boo = function(){};>>>typeof boo.prototype
returns "object"
Prototypes can be augmented
>>> boo.prototype.a = 1;
>>> boo.prototype.sayAh = function(){};
Prototypes can be overwritten
Prototype Property and Dynamic inheritance:
The functions in JavaScript are objects and prototype is one of is property.
- You cause a class to inherit using
ChildClassName.prototype = new ParentClass();. - You need to remember to reset the
constructorproperty for the class usingChildClassName.prototype.constructor=ChildClassName. - You can call ancestor class methods which your child class has overridden using the
Function.call()method. - Javascript does not support protected methods.
Eg.
>>>var boo = function(){};>>>typeof boo.prototype
returns "object"
Prototypes can be augmented
>>> boo.prototype.a = 1;
>>> boo.prototype.sayAh = function(){};
Prototypes can be overwritten
>>> boo.prototype = {a: 1, b: 2};
How is the prototype used?
when a function is invoked as a constructor
var Person = function(name) {
this.name = name;
};
Person.prototype.say = function() {
return this.name;
}
>>> var dude = new Person('dude');
>>> dude.name;
"dude"
>>> dude.say();
"dude"
say() is a property of the prototype object
but it behaves as if it's a property of the dude object
How is the prototype used?
when a function is invoked as a constructor
var Person = function(name) {
this.name = name;
};
Person.prototype.say = function() {
return this.name;
}
>>> var dude = new Person('dude');
>>> dude.name;
"dude"
>>> dude.say();
"dude"
say() is a property of the prototype object
but it behaves as if it's a property of the dude object
Can we tell the difference? Own properties vs. prototype’s:
>>> dude.hasOwnProperty('name');
true
>>> dude.hasOwnProperty('say');
false
isPrototypeOf():
>>> Person.prototype.isPrototypeOf(dude);
true
>>> Object.prototype.isPrototypeOf(dude);
true
__proto__:
__proto__ is a secret link to the prototype The secret link is exposed in Firefox it does not exist in Internet Explorer,
__proto__ is not the same as prototype. __proto__ is a property of the instances, whereas prototype is a property of the constructor functions.
>>> dude.__proto__.hasOwnProperty('say')
true
>>> dude.prototype
??? // Trick question
>>> dude.__proto__.__proto__.hasOwnProperty('toString')
true
__proto__ is a secret link to the prototype The secret link is exposed in Firefox it does not exist in Internet Explorer,
__proto__ is not the same as prototype. __proto__ is a property of the instances, whereas prototype is a property of the constructor functions.
>>> dude.__proto__.hasOwnProperty('say')
true
>>> dude.prototype
??? // Trick question
>>> dude.__proto__.__proto__.hasOwnProperty('toString')
true
In Object oriented client side programming prototype property is highly used.Inheretence can be achieved by prototyping.
Great
ReplyDeleteOnline JavaScript Training
Online JavaScript Training from India