Given the following code:
function a() {}
function b() {}
b.prototype = new a();
var b1 = new b();
We can stay that a
has been add
Well, the prototype link between objects ([[Prototype]]
) is internal, some implementations, like the Mozilla, expose it as obj.__proto__
.
The Object.getPrototypeOf method of the ECMAScript 5th Edition is what you're needing, but it isn't implemented right now on most JavaScript engines.
Give a look to this implementation by John Resig, it has a pitfall, it relies on the constructor
property of engines that don't support __proto__
:
if ( typeof Object.getPrototypeOf !== "function" ) {
if ( typeof "test".__proto__ === "object" ) {
Object.getPrototypeOf = function(object){
return object.__proto__;
};
} else {
Object.getPrototypeOf = function(object){
// May break if the constructor has been tampered with
return object.constructor.prototype;
};
}
}
Remember that this is not 100% reliable, since the constructor
property is mutable on any object.