Yet another javascript class inheritance library
Just include <script src="path/to/class.js"></script>
in your html.
Run npm install yac
in the console, and declare var Class = require("yac")
in the code to use it.
To define a class just use Class.extend
passing the properties and functions like a plain javascript object:
var Cat = Class.extend({
init: function(name) {
this.name = name;
},
say: function(message) {
return "Cat " + this.name + " says: " + message;
}
});
var garfield = new Cat("Garfield"); // Calls Cat#init and set the "Garfield" as the name
garfield.say("meow"); // returns "Cat Garfield says: meow";
garfield instanceof Cat; // returns true
Subclasses are created calling extend
in the parent class. When a function is already defined you can override it and invoke the parent class implementation using this._super(args)
:
var WildCat = Cat.extend({
say: function(message) {
return this._super(message.toUpperCase() + "!");
}
});
var wildcat = new WildCat("Tom");
wildcat.say("meow"); // returns "Cat Tom says: MEOW!"
extend
also supports mixins. Any object passed as parameter will be merged in the left to right direction:
var Pokemon = {
getId: function() {
return this.id;
},
getType: function() {
return this.type;
}
}
var Meowth = Cat.extend(Pokemon, {
init: function() {
this._super("Meowth"); // Calls the super constructor (Cat#init), which set the cat name as "Meowth"
this.id = 52;
this.type = "normal";
}
});
var meowth = new Meowth();
meowth.say("meow"); // returns "Cat Meowth says: meow";
meowth.getId(); // returns 52
meowth.getType(); // returns "normal"
Defines methods at class level
var User = Class.extend();
User.overrideClass({
all: function() {
return $.ajax('/users.json');
}
});
User.all().then(function(users) {
console.log(users);
});
You can also can pass multiple mixins, like in extend
var Searchable = {
all: function() {
return $.ajax(this.path + '.json');
}
}
var User = Class.extend();
var Post = Class.extend();
User.overrideClass(Searchable, {
path: '/users'
});
Post.overrideClass(Searchable, {
path: '/posts'
});
User.all().then(function(users) {
console.log(users);
});
Post.all().then(function(posts) {
console.log(posts);
});
Run npm start
and go to http://localhost:4200/test.html to run the tests in the browser
Use npm test
to run the tests in the console
Git clone the repo and run npm run-script build
to build a production source in the dist
folder
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request