Let's summarize the most important topics you have learned in this chapter.
All functions have a property called
prototype
. Initially it contains an empty object.You can add properties and methods to the prototype object. You can even replace it completely with an object of your choice.
When you create objects using a function as a constructor (with
new
), the objects will have a secret link pointing to their prototype, and can access the prototype's properties as their own.Own properties take precedence over prototype's properties with the same name.
Use the
hasOwnProperty()
method to differentiate between own properties and prototype properties.There is a prototype chain: if your object
foo
doesn't have a propertybar
, when you dofoo.bar
, JavaScript will look for abar
property of the prototype. If none is found, it will keep searching in the prototype's prototype, then the prototype of the prototype's prototype and keep going all the way up to the highest-level parentObject
....