1.1.0 • Published 7 years ago
inheritance-es5 v1.1.0
inheritance-es5
Inheritance in JavaScript (ES5)
What is this library objective?
Inheritance in ES5, is quite obvious, isn't it?
- Inheritance of methods.
- Inheritance of properties.
- Use of instanceof (prototype chain).
- Call super methods (
__super__
). - Allow method overwriting.
- Allow property overwriting.
How to use inheritance-es5
Write your javascript modules as usual.
/** * Feel like a god creating animals * @param {Object} options * @constructor * @class */ function Animal(options) { var opts = options || {}; this._name = opts.name; } Animal.prototype = { eat: function() { return (this.name || this.type) + ' is eating'; } }; /** * Brings a new kitten to the world * @param options * @constructor * @class */ function Cat(options) { var opts = options || { isSphynx: false }; this.isSphynx = typeof opts.isSphynx === 'boolean' && opts.isSphynx; } Cat.prototype = { hasFur: function() { return !this.isSphynx; } };
Apply inheritance after definition.
inheritance(Cat, Animal); // or Cat = inheritance(Cat, Animal);
Call super constructor (if needed)
function Cat(options) { var opts = options || { isSphynx: false }; this.isSphynx = typeof opts.isSphynx === 'boolean' && opts.isSphynx; Cat.__super__(this, [opts]); // super_constructor(opts) }
Done! Easy, huh?
var haku = new Cat({name: 'Haku'}); haku.eat(); // Haku is eating!
TODOs
- Readable test suite.
- If a property is not configurable, then it can not be overwritten.
1.1.0
7 years ago
1.0.2
7 years ago
1.0.1
7 years ago
1.0.0-semantically-release
7 years ago
0.0.0-semantically-release
7 years ago
0.0.0
7 years ago
0.0.0-development
7 years ago
1.0.0
7 years ago