Java Reference
In-Depth Information
The Prototype Is Live!
The prototype object is live, so if a new property or method is added to the prototype,
any instances of it will inherit the new properties and methods automatically, even if that in-
stance has already been created. For example, we saw that the raph object had a weapon
property and attack() method that it inherited from the prototype object. But the
leo object that we created before we added these to the prototype will also gain access to
them as well:
leo.weapon;
<< "Hands"
leo.attack();
<< "Leonardo hits you with his Hands"
If we now change the value of the prototype's weapon property, this will be reflected in
all instances of the Turtle constructor:
Turtle.prototype.weapon = "Feet";
<< "Feet"
leo.attack();
<< "Leonardo hits you with his Feet"
raph.attack();
<< "Raphael hits you with his Feet"
don.attack();
<< "Donatello hits you with his Feet"
Warning: Redefining the Prototype
It is possible to completely redefine the prototype object by assigning it to a
new object literal:
Turtle.prototype = {
attack = function() {
Search WWH ::




Custom Search