-
-
Save xavierm02/1022883 to your computer and use it in GitHub Desktop.
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; | |
}; | |
} | |
}( ) ); | |
} |
This is incorrect:
// child inherits directly from parent. this makes sense.
console.log(Object.getPrototypeOf2(child) === parent); // false in ie 8 or less
Child is the prototype of child, with Parent being the prototype of Child
It needs the constructor property to work properly.
What makes this work consistently is to enforce it with an Object.create polyfill that adds proto to all newly created objects when called, that way your else never gets used, but I suppose I am digressing...
When you say proto you mean proto ? Because there are some browser supproting proto and not Object.create. So it would create some weird bugs.
And if it is proto then you need to add an else if.
if you polyfill Object.create to add the proto then you won't hit the else above.
You'd still need it to work on objects created with the new operator.
This was a top Google result for "getProtoTypeOf polyfill". I think your updating the description will really help people who stumble across it, so thanks!