What's the difference between isPrototypeOf and instanceof in Javascript?

what does
what w
how to pronounce what
whats the meaning of
what is love
what c

In some of my own older code, I use the following:

Object.prototype.instanceOf = function( iface )
 return iface.prototype.isPrototypeOf( this );

Then I do (for example)

[].instanceOf( Array )

This works, but it seems the following would do the same:

[] instanceof Array

Now, surely this is only a very simple example. My question therefore is:

Is a instanceof b ALWAYS the same as b.prototype.isPrototypeOf(a) ?

Yes, they do the same thing, both traverse up the prototype chain looking for an specific object in it.

The difference between both is what they are, and how you use them, e.g. the isPrototypeOf is a function available on the Object.prototype object, it lets you test if an specific object is in the prototype chain of another, since this method is defined on Object.prototype, it is be available for all objects.

instanceof is an operator and it expects two operands, an object and a Constructor function, it will test if the passed function prototype property exists on the chain of the object (via the [[HasInstance]](V) internal operation, available only in Function objects).

For example:

function A () {
  this.a = 1;
function B () {
  this.b = 2;
B.prototype = new A();
B.prototype.constructor = B;

function C () {
  this.c = 3;
C.prototype = new B();
C.prototype.constructor = C;

var c = new C();

// instanceof expects a constructor function

c instanceof A; // true
c instanceof B; // true
c instanceof C; // true

// isPrototypeOf, can be used on any object
A.prototype.isPrototypeOf(c); // true
B.prototype.isPrototypeOf(c); // true
C.prototype.isPrototypeOf(c); // true

What, a punishment or reprimand (esp in the phrase give (a person) what for) See full dictionary entry for what. Quickly send and receive WhatsApp messages right from your computer.

What for definition and meaning, what as a pronoun, it can be the subject, object, or complement of a verb. It can also be the object of a preposition. noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.

Operator precedence and truthiness differ since one is an expression and the other is a method call. One thing to emphasize is that both traverse the prototype chain, so you cannot assume that there is a one-to-one mapping between a matching prototype and the object in question:

var i = 0;

function foo()
console.log(i++ + ": " + Object.prototype.isPrototypeOf(Object) ) //true
console.log(i++ + ": " + Function.prototype.isPrototypeOf(Function) ) //true

console.log(i++ + ": " + Function.prototype.isPrototypeOf(Function) ) //true
console.log(i++ + ": " + Function.prototype.isPrototypeOf(Object) ) //true

console.log(i++ + ": " + RegExp.prototype.isPrototypeOf( RegExp(/foo/) ) ) //true
console.log(i++ + ": " + Object.prototype.isPrototypeOf( RegExp(/foo/) ) ) //true
console.log(i++ + ": " + Function.prototype.isPrototypeOf( RegExp(/foo/) ) ) //false
console.log(i++ + ": " + Object.prototype.isPrototypeOf(Math) ) //true
console.log(i++ + ": " + Math.isPrototypeOf(Math) ) //false

function bar()
console.log(i++ + ": " + (Object instanceof Object) ) //true

console.log(i++ + ": " + (Function instanceof Function) ) //true
console.log(i++ + ": " + (Function instanceof Object) ) //true

console.log(i++ + ": " + (RegExp(/foo/) instanceof RegExp) ) //true
console.log(i++ + ": " + (RegExp(/foo/) instanceof Object)  ) //true
console.log(i++ + ": " + (RegExp(/foo/) instanceof Function) ) //false
console.log(i++ + ": " + (Math instanceof Object) ) //true
console.log(i++ + ": " + (Math instanceof Math) ) //error

What, what also have various functions. It can be used as a adjective, an adverb, a pronoun, or an interjection. what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more.

What Part of Speech is “WHAT”, is in the sentence, "Take what clothes you need." whatever: adverb at all , of any description , of any kind or sort , whatsoever , whichever

What dictionary definition, I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)  From Middle English what, from Old English hwæt (“what”), from Proto-Germanic *hwat (“what”), from Proto-Indo-European *kʷód (“what”), neuter form of *kʷós (“who”).

what, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. what definition: The definition of what is which one or as many as. (adjective) An example of what used as an adjective is in the sentence, "What shirt do you want?"

  • While you can always use instanceof (with constructors on the RHS), not all objects might inherit from Object.prototype. Object.create(null) instanceof Something and ({}).instanceOf({prototype:Something.prototype}) will work (and yield false) where the reverse would fail.
  • So, the only difference is, that I can use isPrototypeOf, if I have only the prototype, whereas I need the constructor for instanceof? (Making my function really identical to instanceof?)
  • Your text answer is useful, your code example is less. (It does only show equal aspects but no differences.) However it raises another question to me: That is ".constructor" for? I've seen such code in some places, but never wrote that myself and I don't seem to have needed it. (I usually had something like C.prototype.clazz = C; tough.) Why do people set constructor?
  • @Steffen Heil: the code written by CMS is clear enough I think, it uses the most simple and known (though it is not the most effective) way to inherit in JavaScript between constructors. Try to delete the line B.prototype.constructor = B and inspect the constructor of an instance of B: alert((new B).constructor) and you'll see the A function constructor. By doing that, he assures himself to find, as a constructor of all the instances of B, just B.
  • @CMS Can you also say, instanceof is mainly for pseudo-classical inheritance, as it relies on B.prototype, and B, which is a "constructor". So what if B.prototype and B are not used, so there is no constructor, when the JavaScript program is written strictly in a prototypal inheritance way? In this case, then animal.isPrototypeOf(woofie) can be used
  • AFAIK instanceof doesn't work when not using a constructor function, e.g. Object.create. Seems to have all kind of other problems as well: tobyho.com/2011/01/28/checking-types-in-javascript
  • Note that the issue encountered in Difference #2 could also be avoided by using Object.prototype.isPrototypeOf.call(B.prototype, a)
  • see quora.com/…