Created
November 23, 2010 01:09
-
-
Save creationix/711061 to your computer and use it in GitHub Desktop.
A probably evil hack to get working super in any function.
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
// 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