UNPKG

1.23 kBJavaScriptView Raw
1'use strict';
2
3var _lodash = require('lodash');
4
5// Uses a hash of prototype properties and class properties to be extended.
6module.exports = function extend(protoProps, staticProps) {
7 var Parent = this;
8
9 // The constructor function for the new subclass is either defined by you
10 // (the "constructor" property in your `extend` definition), or defaulted
11 // by us to simply call the parent's constructor.
12 var Child = protoProps && protoProps.hasOwnProperty('constructor') ? protoProps.constructor : function () {
13 return Parent.apply(this, arguments);
14 };
15
16 (0, _lodash.assign)(Child, Parent, staticProps);
17
18 // Set the prototype chain to inherit from `Parent`.
19 Child.prototype = Object.create(Parent.prototype, {
20 constructor: {
21 value: Child,
22 enumerable: false,
23 writable: true,
24 configurable: true
25 }
26 });
27
28 if (protoProps) {
29 (0, _lodash.assign)(Child.prototype, protoProps);
30 }
31
32 // Give child access to the parent prototype as part of "super"
33 Child.__super__ = Parent.prototype;
34
35 // If there is an "extended" function set on the parent,
36 // call it with the extended child object.
37 if ((0, _lodash.isFunction)(Parent.extended)) Parent.extended(Child);
38
39 return Child;
40};
\No newline at end of file