Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need the isPrototypeOf at all?

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)?

like image 575
Pacerier Avatar asked May 12 '11 03:05

Pacerier


People also ask

What is isPrototypeOf?

isPrototypeOf() The isPrototypeOf() method checks if an object exists in another object's prototype chain.

How to check prototype of object?

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.

What is purpose of prototype in node JS?

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.

What is the difference between __ proto __ and prototype?

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.


1 Answers

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 the prototype.

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)

Further reading

  • constructor @ MDC
  • What it the significance of the Javascript constructor property?
  • OO Programming in JS
  • Constructors considered mildly confusing

Edit re: OP edit

Addtionally, is p.isPrototypeOf(o) functionally equivalent to p===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)

15.2.3.2 Object.getPrototypeOf

15.2.4.6 Object.prototype.isPrototypeOf

like image 177
Matt Ball Avatar answered Sep 24 '22 00:09

Matt Ball