Skip to content

Instantly share code, notes, and snippets.

@turbolent
Created March 11, 2011 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turbolent/866506 to your computer and use it in GitHub Desktop.
Save turbolent/866506 to your computer and use it in GitHub Desktop.
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