This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klass.js
83 lines (71 loc) · 2.37 KB
/
klass.js
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
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('../utils/common');
/**
* Shared empty constructor function to aid in prototype-chain creation.
*/
var ctor = function(){};
/**
* Helper function to correctly set up the prototype chain, for subclasses.
* Similar to `goog.inherits`, but uses a hash of prototype properties and
* class properties to be extended.
* Took it from Backbone.
* @param {Object} parent
* @param {Object} protoProps
* @param {Object} staticProps
* @returns {Object}
*/
function inherits(parent, protoProps, staticProps) {
var child;
// The constructor function for the new subclass is either defined by
// you (the "constructor" property in your `extend` definition), or
// defaulted by us to simply call the parent's constructor.
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = protoProps.constructor;
} else {
child = function() {
parent.apply(this, arguments);
};
}
// Inherit class (static) properties from parent.
utils.extend(child, parent);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
ctor.prototype = parent.prototype;
child.prototype = new ctor();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps)
utils.extend(child.prototype, protoProps);
// Add static properties to the constructor function, if supplied.
if (staticProps)
utils.extend(child, staticProps);
// Correctly set child's `prototype.constructor`.
child.prototype.constructor = child;
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
}
return {
/**
* The self-propagating extend function for classes.
* Took it from Backbone
* @param {Object} protoProps
* @param {Object} classProps
* @returns {Object}
*/
extend: function(protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
// a hack required to WSH inherit `toString` method
if (protoProps.hasOwnProperty('toString'))
child.prototype.toString = protoProps.toString;
return child;
}
};
});