Skip to content
Make one class (constructor function) inherited from another
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
test
.gitignore Add isSubclass method, Gruntfile, UMD distribution, jam and bower Sep 30, 2013
.travis.yml Add tests, travis-ci integration Jan 12, 2014
Gruntfile.js Add jspm config Mar 17, 2015
History.md
Makefile
README.md
bower.json
component.json
index.js
package.json

README.md

extend

Make one class (constructor function) inherited from another.

Based on extend method from YUI library.

Build Status Built with Grunt

Installation

Component

component install gamtiq/extend

Jam

jam install extend

JSPM

jspm 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");
...

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

You can’t perform that action at this time.