I have been looking through some shim/polyfill libraries and see that some of them have a shim for Object.getPrototypeOf
. On fail to exist they fall through to using __proto__
and if that doesn't exist then to object.constructor.prototype
.
I understand that __proto__
is "non-standard" and while slightly different to the Object.getPrototypeOf
method, they can be pretty interchangeable.
I also understand that in principle the externally accessible object.constructor.prototype
would suffice in many situations where neither of the other two existed (provided the prototype has not been reassigned).
Where I have a problem is with the following examples:
function findPropertyOwner(object, property) {
var count = 0;
do {
if (object.hasOwnProperty(property)) {
return [object, count];
}
object = Object.getPrototypeOf(object);
count += 1;
} while (object);
return undefined;
}
Or
function instanceOf(object, constructor) {
while (object) {
if (object === constructor.prototype) {
return true;
}
object = Object.getPrototypeOf(object);
}
return false;
}
With such examples as above where we "walk the chain" so to speak, if the shim falls back to object.constructor.prototype
then we now end up in the horrible situation of an infinite loop.
My question: Is there any way to achieve the above code in an environment where Object.getPrototypeOf
and __proto__
do not exist?
My feeling is that there is not, but I just want to check in case there is some information out there that I have not come across.