๐Ÿ“ฆ gamtiq / extend

Make one class (constructor function) inherited from another

โ˜… 9 stars โ‘‚ 3 forks ๐Ÿ‘ 9 watching
๐Ÿ“ฅ Clone https://github.com/gamtiq/extend.git
HTTPS git clone https://github.com/gamtiq/extend.git
SSH git clone git@github.com:gamtiq/extend.git
CLI gh repo clone gamtiq/extend
Denis Sikuler Denis Sikuler Add info about usage with Duo and JSPM 0d2b5df 10 years ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ dist
๐Ÿ“ test
๐Ÿ“„ .gitignore
๐Ÿ“„ .travis.yml
๐Ÿ“„ bower.json
๐Ÿ“„ component.json
๐Ÿ“„ Gruntfile.js
๐Ÿ“„ History.md
๐Ÿ“„ index.js
๐Ÿ“„ Makefile
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ 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