Skip to content
robrobbins edited this page Sep 10, 2013 · 5 revisions

Using Object.create

When you wish to make a subclass of an existing sudo Class Object you must do two things:

  1. Create a "constructor" function for your new subclass

  2. Use the Object.create method to inherit the prototype of the Class Object you want

    // the 'constructor' var Foo = function(el, data) { // works because sudo.View used the 'sudo.inherit' method this.construct(el, data); }; // clone the View Class Object's prototype Foo.prototype = Object.create(sudo.View.prototype); // add your code Foo.prototype.bar = function(baz) {...};

How This Works

There are two noteworthy things in the above example. One, we can call to the "super constructor" of our Foo View subclass via the this.construct() method. Two, in order to inherit the methods on the sudo.View prototype we must "clone" it with the Object.create method.

A Constructor Caveat

If you were to subclass the Foo Class Object above the same way it suclassed sudo.View with a Bar class, that Bar class would not inherit the constructor Foo. This is a point of great confusion, but it's easy to understand:

** A "Constructor" function is not on its own prototype**

Clone this wiki locally