This repository was archived by the owner on Jul 19, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcreate-class.js
More file actions
82 lines (64 loc) · 1.97 KB
/
Copy pathcreate-class.js
File metadata and controls
82 lines (64 loc) · 1.97 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
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
(function (global) {
var functionReserved = ['length', 'name', 'arguments', 'caller', 'prototype', 'extend', 'extendClass'];
function bind (func, obj) {
return function () {
return func.apply(obj, arguments);
};
}
function noSuper() {
throw new Error('No "super" method defined');
}
function extend (dest, source) {
for (var key in source) {
if (source.hasOwnProperty(key)) {
var sourceProp = source[key];
if (typeof sourceProp == 'function') {
if (functionReserved.indexOf(key) > -1) {
continue;
}
var type = typeof dest[key];
// if we will replace method or undefined with method - we should wrap it
if (type == 'undefined' || type == 'function') {
var originalFunc = dest[key];
var newFunc = sourceProp;
sourceProp = (function (of, nf) {
return function () {
var oldSuper = this._super;
this._super = of ? bind(of, this) : noSuper;
var result = nf.apply(this, arguments);
this._super = oldSuper;
return result;
};
})(originalFunc, sourceProp);
}
}
dest[key] = sourceProp;
}
}
}
function createClass (superClass, instanceProps, classProps) {
function c () {
if (this.init) {
return this.init.apply(this, arguments);
}
}
c.extend = function (instanceProps) {
extend(c.prototype, instanceProps);
};
c.extendClass = function (classProps) {
extend(c, classProps);
};
if (superClass) {
c.prototype = Object.create(superClass.prototype);
c.extendClass(superClass); // add class properties from superClass
}
if (instanceProps) {
c.extend(instanceProps);
}
if (classProps) {
c.extendClass(classProps);
}
return c;
}
global.createClass = createClass;
})(this);