-
Notifications
You must be signed in to change notification settings - Fork 3
/
Class.js
51 lines (45 loc) · 1.43 KB
/
Class.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
function subclass(superConstructor, makeConstructor, prototype) {
var constructor = makeConstructor(superConstructor);
constructor.prototype = Object.create(superConstructor.prototype);
constructor.prototype.constructor = constructor;
if (typeof prototype == "function")
prototype = prototype.call(constructor.prototype, superConstructor.prototype);
if (Object(prototype) === prototype && constructor.prototype !== prototype)
Object.extend(constructor.prototype, prototype);
return constructor;
}
function composeConstructor(child, parent) {
// maybe just .curry()?
if (arguments.length < 2)
return function(parent) { return composeConstructor(child, parent); };
function constructor() {
var that = parent.apply(this, arguments);
if (!that || Object(that) !== that) that = this;
return child.apply(that, arguments);
}
constructor.prototype = child.prototype;
constructor.prototype.constructor = constructor;
return constructor;
}
/* Example usage:
function Animal(name) {
this.name = name;
}
Animal.prototype.greet = function() {
return "Hi, I'm "+this.name;
}
var Cat = subclass(Animal, composeConstructor(function(name, color) {
this.color = color;
}), function(super) { // You might as well pass an object literal here
this.greet = function() {
return super.greet.call(this)+" and my fur is "+this.color;
};
// or just
return {
shout: function() {
return "Meow!";
}
};
});
var cat = new Cat("Garfield", "orange");
*/