Object Oriented Javascript - 2 : Prototype
continuing Object Oriented Javascript - 1 : Object
Prototype
Running the code below gives an alert showing undefined
var car = {
name:”i20”,
manf:”hyundai”,
exprice:650000,
tax:100000,
getOnRoadPrice:function(){
// this refers to instance car
return this.format(this.exprice + this.tax);
},
format: function(amount){
return amount.fixed(0);
}
};
alert(car.prototype);
jsbin
Now let us explore car
using Javascript console. See the screenshots attached below.
Here we can see there is no property prototype
for car
. But you can see the property __proto__
for car
. So questions are?
- What is
Prototype
? - Why there is a property
prototype
for Object ? - Why there is no property
prototype
for car ? - What is
car.__poto__
?
To find answer to these questions, let us explore Object
and car
once again. See the screenshot attached below.
From this we can understand that
Object.prototype
has the definition of some functionscar
is like the instance ofObject
(When compared to Class based OO Concepts)- When
Object
is instantiated incar
, all the functions inObject.prototype
is added tocar
- So
car
has the behavior defined inObject.prototype
- Any function
Object.prototype.func
can be invoked oncar
by callingcar.func
Now we have answer to our questions
What is
Prototype
?Prototype
is the definition of a behavior. APrototype
is thus a set of functions which results in defining a behavior.Why there is a property
prototype
forObject
?Object
is the type in Javascript which can be instantiated. APrototype
is needed to define the behavior of the instance.Object.prototype
defines this behavior.Why there is no property
prototype
forcar
?car
is an instance of Object here. It just re-uses the prototype of Object and does not own any re-usable prototype.What is
car.__proto__
?__proto__
is what connects an instance to its originalPrototype
. So herecar.__proto__
refers toObject.prototype
Re-using behvaior
Obviously there comes a question about “Re-using?”. In Javascript almost everything is a dynamic object (yes, “almost” because there are exceptions). Even a prototype is a dynamic object. We can change it at any time and the changes will reflect in all the instances those were created before making the change. Let us try this out
var car = {
name: “i20”,
manf: “hyundai”,
showManufacturer:function(){
alert(this.manf);
}
};
car.showManufacturer();
jsbin
An alert will show hyundai
We know that car
was instantiated using Object.prototype
. Let us try modifying it.
var car = {
name: “i20”,
manf: “hyundai”,
showManufacturer:function(){
alert(this.manf);
}
}
Object.prototype.showName = function(){
var msg = this.name || “name not set”;
alert(msg);
}
var car2 = {
name: “i10”,
manf: “hyundai”,
showManufacturer:function(){
alert(this.manf);
}
}
car.showName();
car2.showName();
jsbin
First it shows
i20
, and then i10
So we modified Object.prototype
after creating the instance car
. Then we created one more instance car2
. Then we invoked showName
for both the instances and it worked for both. So the point is when an instance gets created it is not cloning a prototype, but the prototype gets linked to that instance. or the instance sets that prototype as its behavior and starts using it. All the instances re-use the same prototype. So whatever changes we make to the prototype will be visible to all instances. Did you ask how it is possible ? Yes it is possible in Javascript because the instance of a function can be dynamically set. Next
diode.codes/object-oriented-javascript-3/)