Doing JavaScript inheritance with classy
Along with the release of ZippyUI I have also released classy, which is a JavaScript module for writing smart classes in JavaScript today. It’s also available on npm.
classy
is very flexible and can help you do inheritance, mixins, overriding and some other useful stuff. But in this article I’ll be focusing on inheritance and how it helps you write clear code.
Be sure to check out the classy readme to get an overview of what classy can do for you.
Defining classes
Now let’s talk inheritance.
We’ll take a simple example, and create a shape class, with a rectangle subclass.
|
|
Now let’s create a rectangle
|
|
In the code above notice we use classy.define
in order to define a new class. Every class can get an alias
, which is a string identifier associated to the class. Since we specify alias: 'shape'
for the Shape class, we can do
|
|
But whenever an alias is expected, a reference to the class is also accepted. So the code above is the same as
|
|
Retrieving classes
Whenever you define a new class and specify it’s alias, you can always get a reference to that class later using classy.getClass
|
|
Any class created by classy
has a reference to the super class. Access it using MyClass.$superClass
Example:
|
|
Any object instance created as an instance of a classy
class has a reference to both the own class that created it and to the super class.
Example:
|
|
Instantiation
You can either create a rectangle instance by directly calling new Rectangle
or by using classy.create
|
|
The classy.create
method calls new
on the class specified as the first parameter, and passes all subsequent parameters to to the constructor.
Let’s define a Square
shape as well
|
|
Calling super methods
Calling super methods is easily accomplished by using callSuper and callSuperWith
. Whenever you want to call the super method with exactly the same params as the current method, just use callSuper
. If you want to provide custom params, you’ll need to use callSuperWith
.
This is very similar with the use of super
in ES6.
Extending functions
You can even extend functions (“classes”) not defined with classy
.
|
|
This gives you a lot of flexibility, and you can seamlessly work with an existing code-base. classy.define
simply creates a new function to be used as a constructor. This function properly inherits the prototype of the super class, and its own prototype is augmented with other properties.
You can even use classy
with ES6 classes since ES6 classes are just a new pattern for writing constructor functions.
Conclusion
classy
classes are fully interoperable with constructor functions and prototypes, and they give you a very nice way to do inheritance and call super methods. classy
is thoroughly tested and carefully written, so it can be used with confidence. Make sure you give it a try, as it’s gaining momentum!