BjartN BjartN - 4 months ago 10
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 ?

Answer

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

if (s1 instanceof SomeObject) { ... }