1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144# extend
Make one class (constructor function) inherited from another.
Based on [extend](http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_extend) method from [YUI library](http://yuilibrary.com).
[](https://travis-ci.org/gamtiq/extend)
[](http://gruntjs.com/)
## Installation
### [Component](https://github.com/componentjs/component)
component install gamtiq/extend
### [Jam](http://jamjs.org)
jam install extend
### [JSPM](http://jspm.io)
jspm install extend
### [Bower](http://bower.io)
bower install extend
### AMD, Node, script tag
Use `dist/extend.js` or `dist/extend.min.js` (minified version).
## Usage
### Component
```js
var extend = require("extend");
...
```
### Duo
```js
var extend = require("gamtiq/extend");
...
```
### Node
```js
var extend = require("./path/to/dist/extend.js");
...
```
### Jam
```js
require(["extend"], function(extend) {
...
});
```
### JSPM
```js
System.import("extend").then(function(extend) {
...
});
```
### AMD
```js
define(["path/to/dist/extend.js"], function(extend) {
...
});
```
### Bower, script tag
```html
<!-- 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
```js
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
* [basespace](https://github.com/gamtiq/basespace)
* [mixing](https://github.com/gamtiq/mixing)
## Licence
MIT