/extend

Make one class (constructor function) inherited from another

Primary LanguageJavaScript

extend

Make one class (constructor function) inherited from another.

Based on extend method from YUI library.

Build Status Built with Grunt

Installation

component install gamtiq/extend
jam install extend
jspm install extend
bower install extend

AMD, Node, script tag

Use dist/extend.js or dist/extend.min.js (minified version).

Usage

Component

var extend = require("extend");
...

Duo

var extend = require("gamtiq/extend");
...

Node

var extend = require("./path/to/dist/extend.js");
...

Jam

require(["extend"], function(extend) {
    ...
});

JSPM

System.import("extend").then(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