views:

223

answers:

2

Hi

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, surly this is only a very simple example. My question therefor is:

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

Regards, Steffen

+3  A: 

This might help:

"isPrototypeOf differs from instanceof operator. In the expression object instanceof AFunction, the object prototype chain is checked against AFunction.prototype, not against AFunction itself"

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/IsPrototypeOf

Jeremy
Sorry, it didn't. I have read this earlier, and it seems to describe what isPrototypeOf does, but I am not sure I understand it correctly. Especially it does not describe any difference to instanceOf - except for the different way to call it. Hence my question here.
Steffen Heil
As it states, the difference is that `instanceof` checks the prototype chain and does not include the given function. Whereas `isPrototypeOf` checks the prototype chain **as well as** the given function. At least that's my understanding. I could be wrong?
Jeremy
+3  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
CMS
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?)
Steffen Heil
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