-
-
Save subtleGradient/1052392 to your computer and use it in GitHub Desktop.
Polyfill for Object.getPrototypeOf
This file contains hidden or 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
if (typeof Object.getPrototypeOf != "function")(function(){ | |
Object.getPrototypeOf = | |
(typeof "".__proto__ == "object") | |
? function(object){ | |
return getPrototypeValue(object, '__proto__'); | |
} | |
: function(object){ | |
return getPrototypeValue(object, 'constructor').prototype; | |
} | |
; | |
var hasOwnProperty = Object.prototype.hasOwnProperty; | |
function getPrototypeValue(object, propertyName){ | |
try{ | |
if (hasOwnProperty.call(object, propertyName)){ | |
var ownValue = object[propertyName]; | |
delete object[propertyName]; | |
} | |
return object[propertyName]; | |
} | |
catch(e){throw e} | |
finally{ | |
object[propertyName] = ownValue; | |
} | |
} | |
}()); |
How about this
if (typeof Object.getPrototypeOf === 'undefined') {
Object.getPrototypeOf = function (obj) {
var t = typeof obj;
if (!obj || (t !== 'object' && t !== 'function')) {
throw new TypeError('not and object');
}
return obj.__proto__;
};
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Even though it might not work for returning the actual prototype, the idea works fine for determining the constructor of the prototype (but beware of the bug that causes object[propertyName] to be assigned to
undefined
if the property did not exist):