Last active
August 29, 2015 13:56
-
-
Save wizard04wsu/8825810 to your computer and use it in GitHub Desktop.
Cross-browser Object.getPrototypeOf()
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 Object.getPrototypeOf() if it's not supported | |
// - if the __proto__ property is not supported either, this may break if anything in the object's prototype chain has been tampered with | |
// - see http://ejohn.org/blog/objectgetprototypeof/ | |
(function (){ | |
"use strict"; | |
function isPrimitive(o){ var t; return o===t || o===null || (t = typeof o)==="number" || t==="string" || t==="boolean"; } | |
if(!Object.getPrototypeOf){ | |
if(typeof("".__proto__) === "object"){ | |
Object.getPrototypeOf = function getPrototypeOf(object){ | |
if(isPrimitive(object)){ | |
throw new TypeError("first argument is not an object"); | |
} | |
return object.__proto__; | |
}; | |
} | |
else{ | |
//this version may break if the prototype chain has been tampered with | |
Object.getPrototypeOf = function getPrototypeOf(object){ | |
if(isPrimitive(object)){ | |
throw new TypeError("first argument is not an object"); | |
} | |
//if(object === Object.prototype){ //doesn't work since `object` may be in a different window | |
if(object === object.constructor.prototype){ | |
return null; | |
} | |
return object.constructor.prototype; | |
}; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment