You can, for the most part, use the instanceof
operator, e.g.
var err = new Error();
if (err instanceof Error) {
alert('That was an error!');
}
See this jsfiddle.
The Mozilla Developer Network (MDN) has more details on the instanceof
operator here, stating that:
The
instanceof
operator tests presence of constructor.prototype in object prototype chain.
Such that, given the following inputs you would get the outputs reflected in the comments:
function C(){} // defining a constructor
function D(){} // defining another constructor
var o = new C();
o instanceof C; // true, because: Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false, because D.prototype is nowhere in o's prototype chain
o instanceof Object; // true, because:
C.prototype instanceof Object // true
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false, because C.prototype is nowhere in o's prototype chain anymore
D.prototype = new C(); // use inheritance
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true