Skip to content

Instantly share code, notes, and snippets.

@creationix
Created November 23, 2010 01:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save creationix/711061 to your computer and use it in GitHub Desktop.
Save creationix/711061 to your computer and use it in GitHub Desktop.
A probably evil hack to get working super in any function.
// Add a handy super call for all objects
MyBaseClass.prototype.super = function () {
var parent = this.__proto__.__proto__;
var caller = arguments.callee.caller;
var fn;
if (caller.prototype === this.__proto__) {
fn = parent.constructor;
} else {
// This is probably quite slow, but it doesn't seem to affect
// performance too much in real tests.
var proto = this.__proto__;
Object.keys(proto).forEach(function (name) {
if (proto[name] === caller) {
fn = parent[name];
}
});
}
return fn.apply(this, arguments);
};
MyBaseClass.adopt = function (child) {
child.__proto__ = this;
child.prototype.__proto__ = this.prototype;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment