Created
March 11, 2011 20:26
-
-
Save turbolent/866506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inherit (_class, superclass) { | |
_class.prototype.__proto__ = superclass.prototype; | |
} | |
function generic (name) { | |
return function () { | |
var _arguments = Array.prototype.slice.call(arguments); | |
var object = _arguments[0]; | |
var methods = []; | |
if (object.constructor.prototype.hasOwnProperty(name)) | |
methods.push(object.constructor.prototype[name]); | |
var proto = object; | |
while ((proto = proto.__proto__)) { | |
if (proto.hasOwnProperty(name)) | |
methods.push(proto[name]); | |
} | |
for (var i = 0; i < methods.length; i++) | |
methods[i]['%next-method'] = methods[i+1]; | |
if (methods.length) | |
methods[0].apply(null, _arguments); | |
}; | |
} | |
function A () {} | |
A.prototype.initialize = function __method__ (object, a) { | |
print("in A.initialize"); | |
print(!__method__['%next-method']); | |
object.a = a; | |
}; | |
function B () {}; | |
inherit(B, A); | |
B.prototype.initialize = function __method__ (object, b) { | |
print("in B.initialize"); | |
__method__['%next-method'](object, "a"); | |
object.b = b; | |
}; | |
function C () {}; | |
inherit(C, B); | |
function D () {}; | |
inherit(D, C); | |
D.prototype.initialize = function __method__ (object, d) { | |
print("in D.initialize"); | |
__method__['%next-method'](object, "b"); | |
object.d = d; | |
}; | |
var initialize = generic('initialize'); | |
var d = new D(); | |
initialize(d, "d"); | |
print(d.d == "d"); | |
print(d.b == "b"); | |
print(d.a == "a"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment