JavaScript_Succinctly
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
y simply memorizing the mechanics of how the chain hierarchy actually works. Refer<br />
back to Chapter 1 if you need a refresher on how property values are resolved.<br />
Why care about the prototype property?<br />
You should care about the prototype property for four reasons.<br />
Reason 1<br />
The first reason is that the prototype property is used by the native constructor functions<br />
(e.g., Object(), Array(), Function(), etc.) to allow constructor instances to inherit<br />
properties and methods. It is the mechanism that <strong>JavaScript</strong> itself uses to allow object<br />
instances to inherit properties and methods from the constructor function's prototype<br />
property. If you want to understand <strong>JavaScript</strong> better, you need to understand how<br />
<strong>JavaScript</strong> itself leverages the prototype object.<br />
Reason 2<br />
When creating user-defined constructor functions, you can orchestrate inheritance the<br />
same way <strong>JavaScript</strong> native objects do. But first you have to learn how it works.<br />
Reason 3<br />
You might really dislike prototypal inheritance or prefer another pattern for object<br />
inheritance, but the reality is that someday you might have to edit or manage someone<br />
else's code who thought prototypal inheritance was the bee's knees. When this<br />
happens, you should be aware of how prototypal inheritance works, as well as how it<br />
can be replicated by developers who make use of custom constructor functions.<br />
Reason 4<br />
By using prototypal inheritance, you can create efficient object instances that all<br />
leverage the same methods. As already mentioned, not all array objects, which are<br />
instances of the Array() constructor, need their own join() methods. All instances<br />
can leverage the same join() method because the method is stored in the prototype<br />
chain.<br />
Prototype is standard on all Function() instances<br />
All functions are created from a Function() constructor, even if you do not directly<br />
invoke the Function() constructor (e.g., var add = new Function('x', 'y',<br />
'return x + z');) and instead use the literal notation (e.g., var add =<br />
function(x,y){return x + z};).<br />
When a function instance is created, it is always given a prototype property, which is<br />
an empty object. In the following sample, we define a function called myFunction and<br />
then access the prototype property which is simply an empty object.<br />
121