Classy - Smart JavaScript classes
Classy offers the ability to easily define classes, call super or overriden methods, define static properties, and mixin objects in a very flexible way.
Meant to be used in the browser and in node. Well tested, with 50+ tests.
Installation
$ npm install classy
For the browser please either use webpack
or browserify
to integrate classy into your app.
Contributing
See CONTRIBUTING.md
Example
var Vehicle = classy var Car = classy var ford = 1980 'Ford'consoleconsolevar bmw = // <- since forceInstance is true, the constructor will be called with new under the hood
Notice the callSuper()
method call, which can be used in any class method, and will call the method with the same name found on the super class. It also automatically sends all the arguments, so you don't have to manually do so.
ford === 'Ford' //is true
classy//nowford === 'Ford, made in 1980' //is true
You can use the class alias
in order to easily reference which class you want to extend or override. This also helps you get a reference to your class by
var Car = classy var Vehicle = classy
Aliases
When defining a class, you can specify a string property named alias
.
classy
keeps a reference to each class based on the specified alias. If no alias is given, one is generated anyway.
Using the alias allows you to reference, extend or override a class by the alias, without the need for an explicit reference to the class.
Example
classy classy classy
Notice that when defining the rectangle class, instead of saying we extend the Shape class, by a direct reference, we can use the alias of the Shape class, which is a string.
Whenever an alias is expected, you can use either the alias, or the class itself (in classy.define, classy.override, classy.getClass, etc)
Override and callOverriden
Overriding is simple, just call classy.override
with the class alias or the class reference as the first param, and an object with properties to override as a second param.
classy
or, if you don't have a reference to the class, but only have the alias
classy
init
as constructor
Use the init
method as the constructor
Example
var Animal = classy var Cat = classy var lizzy = name: 'lizzy'
You can even extend functions/classes not defined with classy
{ thissound = sound} Animalprototype{ return 'I sound like this: ' + thissound} var Dog = classy var dog = dog == 'I sound like this: bark' // is true
callSuper
and callOverriden
Use the callSuper
and callOverriden
methods to call the super and overriden methods. You don't have to worry about forwarding the arguments, since this is handled automagically for you.
If there is no super or overriden method with the same name you don't have to worry either, since callSuper and callOverriden won't break. they will simply and silently do nothing
Example
//create a shape class classy //create a rectangle class with a width and a height classy classy //create a square class classy
You can also use callSuperWith
and callOverridenWith
to manually pass all parameters
Example
//...{ this}//...
Getters and setters
You can use getters and setters on classy
defined classes. They even work well with callSuper
and callOverriden
var Randomer = classy Randomer var r = rrandom // generates a random int between 0 and 10
forceInstance
You may want your classes to be usable without the new
operator. Just specify forceInstance: true
on the class prototype, and the constructor will be called with new
if it hasn't been
Example
var Vehicle = classvar v = // since 'forceInstance' is true, //the Vehicle will be called as a constructor under the hood, so new Vehicle('car')
Mixins
Classy offers the ability to mix objects into other objects. At a base level, you can either use simple objects as mixins or you can define mixin classes.
Example
var logger = $after: { console } var person = firstName: 'Bob' lastName: 'Johnson' classy
in the example above, the person object receives a log function property. Note the usage of $after. Other valid mixin behaviors are $copyIf
, $before
and $override
.
These behaviors determine how mixin properties that are functions are mixed-in when the target object already has those properties.
$copyIf
Any property in the mixin is copied onto the target object only if the target object does not already have a property with the same name
Example
var logger = $copyIf: isLogger: true { console } { console } var person = { } classyperson //will alert 'Hi Bob' - so logger.greet is not copied, since it already existed on personperson //will console.log 'warning' - logger.log was copied to person, since person.log was undefinedpersonisLogger === true
$before & $after
classy var person = { ; return 1} classy person === 1 // will first console.log('hi') and then will alert('hi')//and will return the return value of the initial person.log implementationperson === '!' //will console.warn('hi')
In the above example, since log and warn ar copied with a before behavior, first of all classy checks to see if person already has those properties. Since person.log exists, person.log is assigned another function, which calls logger.log
before person.log
, and returns the result of the initial person.log
.
For logger.warn, no such property exists on person, so it is simply assigned to the person.
The behavior of after is similar, with the difference that the mixin function is called after the initial implementation. The result is that of the initial implementation, if one exists.
$override
classy.defineMixin({
alias: 'logger',
$override: {
log: function(msg){ console.log(msg) },
warn: function(msg){
console.log(msg)
return this.callTarget() //call the target object warn implementation, if one exists
}
}
})
var Person = classy.define({
alias: 'person',
mixins: [
'logger'
],
name: 'bob',
warn: function(msg){
alert(msg)
return this
}
})
var p = new Person()
p.log(p.name) //simply calls logger.log
p.warn(p.name) // logs p.name and then alerts p.name
Notice that logger.warn
calls this.callTarget()
which means the mixin tries to call the method from the target object that this function has overriden. Since person.warn had an implementation, the logger calls that.
$ownClass
Static properties and You can easily define static properties for classes.
var Widget = classy Widget == 'A Widget class' // === true var w = wid === 0 w = wid === 1
On every instance, you can use the $ownClass property in order to get a reference to the class that created the instance.
Building
In order to build a browser version, run npm run build
.
This will use browserify to make a one-file browser build, which you can find in dist/classy.js
Testing
After cloning the repo, make sure you npm install
.
Then just run npm run test
or make
.
Make sure you build before you test, since testing is done on a browser build, with karma test runner. To build, npm run build