forked from ded/klass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
klass.js
91 lines (80 loc) · 2.17 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
84
85
86
87
88
89
90
91
/*!
* klass: a classical JS OOP façade
* https://github.com/ded/klass
* License MIT (c) Dustin Diaz 2014
*/
!function (name, context, definition) {
if (typeof define == 'function') define(definition)
else if (typeof module != 'undefined') module.exports = definition()
else context[name] = definition()
}('klass', this, function () {
var context = this
, f = 'function'
, fnTest = /xyz/.test(function () {xyz}) ? /\bsupr\b/ : /.*/
, proto = 'prototype'
function klass(o) {
return extend.call(isFn(o) ? o : function () {}, o, 1)
}
function isFn(o) {
return typeof o === f
}
function wrap(k, fn, supr) {
return function () {
var tmp = this.supr
this.supr = supr[proto][k]
var undef = {}.fabricatedUndefined
var ret = undef
try {
ret = fn.apply(this, arguments)
} finally {
this.supr = tmp
}
return ret
}
}
function process(what, o, supr) {
for (var k in o) {
if (o.hasOwnProperty(k)) {
what[k] = isFn(o[k])
&& isFn(supr[proto][k])
&& fnTest.test(o[k])
? wrap(k, o[k], supr) : o[k]
}
}
}
function extend(o, fromSub) {
// must redefine noop each time so it doesn't inherit from previous arbitrary classes
function noop() {}
noop[proto] = this[proto]
var supr = this
, prototype = new noop()
, isFunction = isFn(o)
, _constructor = isFunction ? o : this
, _methods = isFunction ? {} : o
function fn() {
if (this.initialize) this.initialize.apply(this, arguments)
else {
fromSub || isFunction && supr.apply(this, arguments)
_constructor.apply(this, arguments)
}
}
fn.methods = function (o) {
process(prototype, o, supr)
fn[proto] = prototype
return this
}
fn.methods.call(fn, _methods).prototype.constructor = fn
fn.extend = arguments.callee
fn[proto].implement = fn.statics = function (o, optFn) {
o = typeof o == 'string' ? (function () {
var obj = {}
obj[o] = optFn
return obj
}()) : o
process(this, o, supr)
return this
}
return fn
}
return klass
});