HTML and CSS Reference
In-Depth Information
8.2.2 Prototypal Inheritance
ECMAScript 5 makes prototypal inheritance in JavaScript more obvious, and avoids
the clumsy constructor convention. In ES3, the only native way to create an object
sphere that inherits from another object circle is by proxying via a constructor,
as Listing 8.5 shows.
Listing 8.5 Create an object that inherits from another object in ES3
"test es3 inheritance via constructors": function () {
var circle={/*...*/};
function CircleProxy() {}
CircleProxy.prototype = circle;
var sphere = new CircleProxy();
Additionally, there is no direct way of retrieving the prototype property in ES3.
Mozilla added a proprietary __ proto __ property that fixes both of these cases, as
in Listing 8.6.
Listing 8.6 Proprietary shortcut to accessing and setting prototype
__ proto __ ": function () {
"test inheritance via proprietary
var circle={/*...*/};
var sphere = {};
sphere. __ proto __ = circle;
The __ proto __ property is not codified by ES5. Instead, two methods were
added to work easily with prototypes. Listing 8.7 shows how we will be doing this
in the future.
Listing 8.7 Creating an object that inherits from another object in ES5
"test inheritance, es5 style": function () {
var circle={/*...*/};
var sphere = Object.create(circle);
Search WWH ::

Custom Search