BjartN BjartN - 10 months ago 46
Javascript Question

Javascript type of custom object

How can I check if my javascript object is of a certain type.

var SomeObject = function() { }
var s1 = new SomeObject();

In the case above
typeof s1
will return "object". That's not very helpful. Is there some way to check if s1 is of type SomeObject ?


Yes, using instanceof (MDN link | spec link):

if (s1 instanceof SomeObject) { ... }