Created
July 18, 2013 03:57
-
-
Save WebReflection/6026633 to your computer and use it in GitHub Desktop.
The most obtrusive and dark trick to promote objects inheritance without actually loosing the current one if not at the deepest level.
It does not pollute the `Object.prototype`
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
var promote = function(getPrototypeOf, setPrototypeOf){ | |
// (C) The Evilness | |
return function(self, Class){ | |
var | |
promote = typeof Class == 'function' ? | |
Class.prototype : getPrototypeOf(Class), | |
current = self, | |
previous, | |
next | |
; | |
if (promote && !promote.isPrototypeOf(self)) { | |
do { | |
previous = current; // {} or latter prototype | |
current = getPrototypeOf(current); // Object.prototype | |
next = current && getPrototypeOf(current); // null | |
} while(next); | |
setPrototypeOf(previous, promote); | |
} | |
return self; | |
}; | |
}( | |
Object.getPrototypeOf || | |
function(o){return o.__proto__}, | |
Object.setPrototypeOf || | |
function(o,p){o.__proto__=p} | |
); |
Author
WebReflection
commented
Jul 18, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment