-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextend.js
More file actions
40 lines (35 loc) · 1.1 KB
/
Copy pathextend.js
File metadata and controls
40 lines (35 loc) · 1.1 KB
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
/**
* Create a new class extending from a parent class
* @param {Function} parentClass The parent class which will be extended
* @param {Object} [childPrototype] The child prototype which will be copied
* @returns {Child}
*/
module.exports = function(parentClass, childPrototype) {
if (typeof parentClass !== 'function') {
throw new Error ('Parent class is not a function');
}
//create the child class
function Child() {
if (childPrototype && childPrototype.construct) {
return childPrototype.construct.apply(this, arguments);
} else {
return parentClass.apply(this, arguments);
}
}
//extend the parent class
if (Object.create) {
Child.prototype = Object.create(parentClass.prototype);
} else {
Child.prototype = new parentClass;
}
Child.prototype.constructor = parentClass;
//copy the prototype methods into the child class
if (childPrototype) {
for (var key in childPrototype) {
if (Object.prototype.hasOwnProperty.call(childPrototype, key)) {
Child.prototype[key] = childPrototype[key];
}
}
}
return Child;
};