There are two differences. As others have mentioned before instanceof
is recursive. You can implement your own version of instanceof
as follows:
function instanceOf(obj, func) {
return Object.isPrototypeOf.call(func.prototype, obj);
}
This function depends upon the availability of the Object.prototype.isPrototypeOf
function.
The second difference is only for browsers. As mentioned in the following answer the actual implementation of instanceof
is as follows in browsers:
function instanceOf(obj, func) {
var prototype = func.prototype;
while (obj = Object.getPrototypeOf(obj)) { //traverse the prototype chain
if (typeof object === "xml") //workaround for XML objects
return prototype === XML.prototype;
if (obj === prototype) return true; //object is instanceof constructor
}
return false; //object is not instanceof constructor
}
That's all. For more information see the documentation of instanceof
: https://developer.mozilla.org/en/JavaScript/Guide/Details_of_the_Object_Model#Determining_instance_relationships