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
|
/*!
* static-extend <https://github.com/jonschlinkert/static-extend>
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var copy = require('object-copy');
var define = require('define-property');
var util = require('util');
/**
* Returns a function for extending the static properties,
* prototype properties, and descriptors from the `Parent`
* constructor onto `Child` constructors.
*
* ```js
* var extend = require('static-extend');
* Parent.extend = extend(Parent);
*
* // optionally pass a custom merge function as the second arg
* Parent.extend = extend(Parent, function(Child) {
* Child.prototype.mixin = function(key, val) {
* Child.prototype[key] = val;
* };
* });
*
* // extend "child" constructors
* Parent.extend(Child);
*
* // optionally define prototype methods as the second arg
* Parent.extend(Child, {
* foo: function() {},
* bar: function() {}
* });
* ```
* @param {Function} `Parent` Parent ctor
* @param {Function} `extendFn` Optional extend function for handling any necessary custom merging. Useful when updating methods that require a specific prototype.
* @param {Function} `Child` Child ctor
* @param {Object} `proto` Optionally pass additional prototype properties to inherit.
* @return {Object}
* @api public
*/
function extend(Parent, extendFn) {
if (typeof Parent !== 'function') {
throw new TypeError('expected Parent to be a function.');
}
return function(Ctor, proto) {
if (typeof Ctor !== 'function') {
throw new TypeError('expected Ctor to be a function.');
}
util.inherits(Ctor, Parent);
copy(Ctor, Parent);
// proto can be null or a plain object
if (typeof proto === 'object') {
var obj = Object.create(proto);
for (var k in obj) {
Ctor.prototype[k] = obj[k];
}
}
// keep a reference to the parent prototype
define(Ctor.prototype, '_parent_', {
configurable: true,
set: function() {},
get: function() {
return Parent.prototype;
}
});
if (typeof extendFn === 'function') {
extendFn(Ctor, Parent);
}
Ctor.extend = extend(Ctor, extendFn);
};
};
/**
* Expose `extend`
*/
module.exports = extend;
|