-
Notifications
You must be signed in to change notification settings - Fork 41
/
turing.oo.js
109 lines (98 loc) · 2.54 KB
/
turing.oo.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*!
* Turing OO
* Copyright (C) 2010-2011 Alex R. Young
* MIT Licensed
*/
/**
* The Turing Class:
*
* User = turing.Class({
* initialize: function(name, age) {
* this.name = name;
* this.age = age;
* }
* });
*
* new User('Alice', '26');
*
* Inheritance:
*
* Pass an object to `turing.Class` to inherit from it.
*
* SuperUser = turing.Class(User, {
* initialize: function() {
* this.$super('initialize', arguments);
* },
*
* toString: function() {
* return "SuperUser: " + this.$super('toString');
* }
* });
*
* Mixins:
*
* Objects can be embedded within each other:
*
* MixinUser = turing.Class({
* include: User,
*
* initialize: function(log) {
* this.log = log;
* }
* });
*
**/
define('turing.oo', ['turing.core'], function(turing) {
var Class, oo;
Class = function() {
return oo.create.apply(this, arguments);
}
oo = {
create: function() {
var methods = null,
parent = undefined,
klass = function() {
this.$super = function(method, args) { return oo.$super(this.$parent, this, method, args); };
this.initialize.apply(this, arguments);
};
if (typeof arguments[0] === 'function') {
parent = arguments[0];
methods = arguments[1];
} else {
methods = arguments[0];
}
if (typeof parent !== 'undefined') {
oo.extend(klass.prototype, parent.prototype);
klass.prototype.$parent = parent.prototype;
}
oo.mixin(klass, methods);
oo.extend(klass.prototype, methods);
klass.prototype.constructor = klass;
if (!klass.prototype.initialize)
klass.prototype.initialize = function(){};
return klass;
},
mixin: function(klass, methods) {
if (typeof methods.include !== 'undefined') {
if (typeof methods.include === 'function') {
oo.extend(klass.prototype, methods.include.prototype);
} else {
for (var i = 0; i < methods.include.length; i++) {
oo.extend(klass.prototype, methods.include[i].prototype);
}
}
}
},
extend: function(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
},
$super: function(parentClass, instance, method, args) {
return parentClass[method].apply(instance, args);
}
};
turing.Class = Class;
turing.oo = oo;
return oo;
});