Java Reference
In-Depth Information
raph.weapon = "Sai";
<< "Sai"
don.weapon = "Bo Staff";
<< "Bo Staff"
These will then become an “own property” of the instance and take precedence over the
same prototype property:
<< "Leonardo hits you with his Katana Blades"
When a property or method is called, the JavaScript engine will check to see if an object
has its own property or method. If it does, it will use that one; otherwise, it will use the
prototype's instead.
What Should the Prototype Be Used For?
The prototype can be used to add any new properties and methods after the constructor
function has been defined. It should be used to define any properties that will remain the
same for every instance of the constructor. The weapon example was unsuitable because
all the turtles use a different weapon (we just needed it to demonstrate overwriting!). They
do, however, like the same food—pizza! This makes a good candidate for a prototype
property: = "Pizza";
Methods are likely to be the same for all instances of a constructor, so all methods should
be part of the prototype object rather than the constructor function itself: = function() {
return "Mmm, this " + + " tastes great!";
Search WWH ::

Custom Search