17.11.2015 Views

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.

Sample: sample119.html<br />

<br />

var myFunction = function () { };<br />

console.log(myFunction.prototype); // Logs object{}<br />

console.log(typeof myFunction.prototype); // Logs 'object'.<br />

<br />

Make sure you completely understand that the prototype property is coming from the<br />

Function() constructor. It is only once we intend to use our function as a user-defined<br />

constructor function that the prototype property is leveraged, but this does not change<br />

the fact that the Function() constructor gives each instance a prototype property.<br />

The default prototype property is an Object() object<br />

All this prototype talk can get a bit heavy. Truly, prototype is just an empty object<br />

property called "prototype" created behind the scenes by <strong>JavaScript</strong> and made available<br />

by invoking the Function() constructor. If you were to do it manually, it would look<br />

something like this:<br />

Sample: sample120.html<br />

<br />

var myFunction = function () { };<br />

myFunction.prototype = {}; // Add the prototype property and set it to an<br />

empty object.<br />

console.log(myFunction.prototype); // Logs an empty object.<br />

<br />

In fact, this sample code actually works just fine, essentially just duplicating what<br />

<strong>JavaScript</strong> already does.<br />

Notes<br />

The value of a prototype property can be set to any of the complex values (i.e. objects)<br />

available in <strong>JavaScript</strong>. <strong>JavaScript</strong> will ignore any prototype property set to a primitive<br />

value.<br />

122

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!