extend_tool
v0.0.1
Published
Make one class (constructor function) inherited from another
Downloads
3
Maintainers
Readme
extend
Make one class (constructor function) inherited from another.
Based on extend method from YUI library.
Installation
Component
component install gamtiq/extend
Jam
jam install extend
Bower
bower install extend
AMD, Node, script tag
Use dist/extend.js
or dist/extend.min.js
(minified version).
Usage
Component
var extend = require("extend");
...
Node
var extend = require("./path/to/dist/extend.js");
...
Jam
require(["extend"], function(extend) {
...
});
AMD
define(["path/to/dist/extend.js"], function(extend) {
...
});
Bower, script tag
<!-- Use bower_components/extend/dist/extend.js if the library was installed via Bower -->
<script type="text/javascript" src="path/to/dist/extend.js"></script>
<script type="text/javascript">
// extend is available via extend field of window object
...
</script>
Example
var SuperClass = function(a, b) {
...
};
SuperClass.prototype.method1 = function(c, d, e) {
...
};
...
var SubClass = function(a, b) {
...
SubClass.superconstructor.call(this, a, b);
// or
// SubClass.superconstructor.apply(this, arguments);
...
};
extend(SubClass, SuperClass);
...
SubClass.prototype.method1 = function(c, d, e) {
...
SubClass.superclass.method1.call(this, c, d, e);
// or
// SubClass.superclass.method1.apply(this, arguments);
...
};
...
if (extend.isSubclass(SubClass, SuperClass)) {
...
}
See test/extend.js
for additional examples.
API
extend(SubClass: Function, ParentClass: Function): Function
Replace value of prototype
field of SubClass
by another object that inherits from prototype
of ParentClass
.
Returns SubClass
.
extend.isSubclass(SubClass: Function, ParentClass: Function): Boolean
Test whether SubClass
is inherited from ParentClass
.
Related projects
Licence
MIT