Repository navigation
Expand file tree
/
Copy pathuber.js
More file actions
executable file
·132 lines (113 loc) · 4.37 KB
/
Copy pathuber.js
File metadata and controls
executable file
·132 lines (113 loc) · 4.37 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function () {
// gets the next prototype up the chain
var getNextPrototype = function (prototype) {
var constructor = prototype.constructor;
delete prototype.constructor;
var nextPrototype = prototype.constructor.prototype;
prototype.constructor = constructor;
return nextPrototype;
},
getPropertyName = function (obj,property) {
for (var propertyName in obj) {
if (obj[propertyName] === property) {
return propertyName;
}
}
return undefined;
},
// get a super method by its sub method
// pass arguments
// requires prototype's to have a reference to their constructor
// use Function.prototype.subclass and Function.prototype.subclasses
getUber = function (args) {
var method = args.callee,
prototype = this.constructor.prototype,
methodName = getPropertyName(this,method),
constructor,
uberMethod;
for (; prototype !== Object; prototype = getNextPrototype(prototype)) {
if (typeof methodName === 'undefined') {
methodName = getPropertyName(prototype,method);
}
if (prototype.hasOwnProperty(methodName)) {
delete prototype[methodName];
uberMethod = prototype[methodName];
prototype[methodName] = method;
if (typeof uberMethod === 'undefined') {
throw new Error('no uber method found');
}
return uberMethod;
}
}
throw new Error('method not found');
},
// pass arguments and optionally an array of args
// if you pass only args, the super method (or constructor) will be called with arguments
// if you pass optional arguments, those will be used to call the uber instead
uber = function (args,optionalArgs) {
return this.getUber(args).apply(this,optionalArgs || args);
},
// used by .subclass and .subclasses
setupInheritance = function (superclass,constructor,oldPrototype) {
// create an empty constructor, used to get an object that inherits from the superclass proto
var C = function () {};
C.prototype = superclass.prototype;
// prototype now inherits from superclass.prototype
var prototype = new C();
// for any properties on oldPrototype, copy them to new prototype
// this will occur if the implementor added properties to the prototype before calling an subclass method
for (var property in oldPrototype) {
// only copy properties that are dircetly on the oldPrototype.
// any old inheritance will be lost.
if (oldPrototype.hasOwnProperty(property)) {
prototype[property] = oldPrototype[property];
}
}
// set the prototype on the constructor
constructor.prototype = prototype;
// give the prototype an explicit reference to its constructor
// this is used to transverse the prototypal chain
prototype.constructor = constructor;
// if uber isnt in the prototype chain, add it now
if (!('uber' in prototype)) {
// add uber and get uber
// this only needs to exist once in the chain
// without this, all classes would have to inherit from a Class class
// instead, they all have 2 methods in common
prototype.uber = uber;
prototype.getUber = getUber;
// since this class's class has to super class, these werent set for it yet
// need this to be set so that we can traverse stuff
superclass.prototype.constructor = superclass;
}
// return the constructor
return constructor;
};
// SubClass.subclasses(SuperClass); // setup a dynamic inheritance
Function.prototype.subclasses = function (superclass) {
return setupInheritance(superclass,this,this.prototype);
};
// var SubClass = SuperClass.subclass({ // returns a constructor with a prototype formed from the object you pass. if your object has a constructor property, that will be returned.
// // optional
// constructor : function () {
// ...
// this.uber(arguments);
// ...
// },
// ...
// });
Function.prototype.subclass = function (properties) {
// if the properties object didnt define a constructor, use a generic one that just calls its uber
var constructor = properties && properties.hasOwnProperty('constructor') ? properties.constructor : function () {
this.uber(arguments);
};
// return the constructor after setting up inheritance
return setupInheritance(this,constructor,properties);
};
Function.klass = function (properties) {
var constructor = properties.hasOwnProperty('constructor') ? properties.constructor : function () {};
properties.constructor = constructor;
constructor.prototype = properties;
return constructor;
};
})();