this page states:
Note: isPrototypeOf differs from instanceof operator. In the expression object instanceof AFunction, the object prototype chain is checked against AFunction.prototype, not against AFunction itself
Ok I don't really get what they are trying to tell us. Isn't object instanceof AFunction
exactly the same as `AFunction.prototype.isPrototypeOf(object)? or am I wrong?
Why do we need the isPrototypeOf
at all?
If i ever need to do p.isPrototypeOf(o)
couldn't I just do o instanceof p.constructor
?
Addtionally, is p.isPrototypeOf(o)
functionally equivalent to p===Object.getPrototypeOf(o)
?
isPrototypeOf() The isPrototypeOf() method checks if an object exists in another object's prototype chain.
The prototype of an object is referred to by the prototype property of the constructor function that creates and initializes the object. The isPrototypeOf() method provides a way to determine if one object is the prototype of another. This technique can be used to determine the class of an object.
A Function object's prototype property is used when the function is used as a constructor with the new operator. It will become the new object's prototype. Note: Not all Function objects have the prototype property — see description.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype.
Object constructors are funky things. From this answer:
As Pointy has pointed out, in his answer
The "constructor" property is a reference to the function that created the object's prototype, not the object itself.
The usual way to deal with this is to augment the object's prototype
constructor
property after assigning to theprototype
.
An object's constructor is not read-only, which is why this is possible to do at all. I could assign any value to p.constructor
after p
is created, and this would completely break using
o instanceof p.constructor
instead of
p.isPrototypeOf(o)
constructor
@ MDCEdit re: OP edit
Addtionally, is
p.isPrototypeOf(o)
functionally equivalent top===Object.getPrototypeOf(o)
?
Those are more similar than your original question, aside from the fact that Object.getPrototypeOf
wasn't introduced until JavaScript 1.8.1? See John Resig - Object.getPrototypeOf
. Perhaps more relevant, the two functions are different in the spec! (warning, PDF link)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With