javascript checking object prototype

javascript, prototype

Solution

`__proto__` isn't part of the ECMAScript 5 specification, and you have no guarantee of future support. The standard ways to access the prototype of an object are to access the prototype of the constructor or, better, Object.getPrototypeOf (but this doesn't work on IE8).

instanceof checks the prototype of the constructor but also "tests presence of constructor.prototype in object prototype chain.".

If your goal is to check if an object is an instance of a specific class, and it's OK for you if it's not a direct instance, then you should use the `instanceof` operator : it's really just made for that.

Problem

Is there any difference between following pieces of code: ``` inst instanceof Constr ``` and ``` inst.__proto__ === Constr.prototype ``` for example: ``` var xyz = {} xyz instanceof Object ``` and ``` xyz.__proto__ === Object.prototype ``` ? If so, what is the difference, which one is the preferred?

Original source

Related problems