create method and the introduction of the new
Why the change? The old system worked nicely for single modules, but was broken when trying to create an extension hierarchy. In the old system the creation of a class and the instantiation of that class were the same thing. In really simplified code that looked something like this:
The main problem with this approach is that every property that has been added to
Animal after the initial
create will become part of the prototype of
Dog as well. Even if those properties are instance properties (e.g.
Animal.bark = true instead of
Animal.prototype.bark = true). This lead to difficult to detect bugs and behaviour.
constructor will become the function that is being called when making an instance with
new. All other methods in the object will become part of the class prototype.
Module.prototype or static properties using direct assignment. Stapes has two convenience methods for this called
proto that allow you to quickly add an object of properties to the prototype or directly to the module.
Inheritance can be done by simply calling
subclass on a newly created class. The
instanceof operator now works correctly too:
Every module now gets a
parent property, referencing the parent’s prototype. You can use this to still call a parent method if you’ve overwritten it.
Note that for backwards compatibility the
create method on the
Stapes global will still work. However, because of the obvious problems it won’t work anymore on submodules. In the future i’ll remove
Stapes.create. If you like the old style i recommend you switch to something like
var module = (Stapes.subclass()).extend() which is more or less the same as the old
So, that’s it. Please leave any questions and remarks in the question section below or file tickets on Github.