Skip to content

Instantly share code, notes, and snippets.

@xavierm02
Created June 13, 2011 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xavierm02/1022883 to your computer and use it in GitHub Desktop.
Save xavierm02/1022883 to your computer and use it in GitHub Desktop.
Polyfill for Object.getPrototypeOf -- It will work unless your constructor's prototype doesn't have a constructor property (which means you assigned another object as prototype and didn't give it a constructor property...)
if ( typeof Object.getPrototypeOf !== "function" ) {
( function ( ) {
function getPrototypeValue( o, p ) {
if ( o.hasOwnProperty( p ) ) {
var ownValue = o[ p ];
if ( delete o[ p ] ) {
var prototypeValue = o[ p ];
o[ p ] = ownValue;
return prototypeValue;
} else {
return o[ p ];
}
} else {
return o[ p ];
}
}
if ( typeof "".__proto__ === "object" ) {
Object.getPrototypeOf = function( object ) {
return getPrototypeValue( object, '__proto__' );
};
} else {
Object.getPrototypeOf = function( object ) {
getPrototypeValue( object, 'constructor' ).prototype;
};
}
}( ) );
}
@xavierm02
Copy link
Author

You'd still need it to work on objects created with the new operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment