Skip to content

Instantly share code, notes, and snippets.

@nw
Forked from creationix/superhack.js
Created November 23, 2010 02:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nw/711126 to your computer and use it in GitHub Desktop.
Save nw/711126 to your computer and use it in GitHub Desktop.
// 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__) {
return parent.constructor.apply(this, arguments);
} else {
// This is probably quite slow, but it doesn't seem to affect
// performance too much in real tests.
var keys = Object.keys(this.__proto__);
for(var i=0, l = keys.length; i < l; i++){
if(this.__proto__[keys[i]] === caller)
return parent[keys[i]].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