1.0.1 • Published 4 years ago
@quintinfsmith/inheritance.js v1.0.1
Inheritance.js
Cleaner OOP in javascript. Initially developed while working for TBM Bookmanager ltd. (www.bookmanager.com) as a way to reduce jQuery usage. Thanks to Michael Neill for allowing me to release it.
Usage
There is jsdoc documentation in inheritance.js, but a quick starter:
define_class("SomeParentClass", {
init: function(some_arg) {
this.some_value = some_arg;
}
});
define_class("SomeChildClass", {
init: function(some_arg) {
this.super('init', some_arg);
}
}, "SomeParentClass");
var child_instance = new SomeChildClass(5);
console.log(child_instance.some_value);
// >> 5
The Compound class is the equivalent to jQuery-ui's widget
define_class("CoolButton", {
init: function(options) {
this.super('init', document.createElement('div'), options);
// the div passed to the super constructor is now "this.element"
this.element.style.backgroundColor = this.options.background_color;
this.element.innerText = this.options.label;
this.element.addEventListener("click", function(event) {
alert("You've clicked the Cool Button");
});
}
}, "Compound");
var button = new CoolButton({ background_color: "#aa00ff", label: "A cool button" });
// Attach the button to the document
document.body.appendChild(button.element);