Javascript type of custom object

Javascript

Javascript Problem Overview


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 ?

Javascript Solutions


Solution 1 - Javascript

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

if (s1 instanceof SomeObject) { ... }

Solution 2 - Javascript

Whatever you do, avoid obj.constructor.name or any string version of the constructor. That works great until you uglify/minify your code, then it all breaks since the constructor gets renamed to something obscure (ex: 'n') and your code will still do this and never match:

// Note: when uglified, the constructor may be renamed to 'n' (or whatever),
// which breaks this code since the strings are left alone.
if (obj.constructor.name === 'SomeObject') {}

Note:

// Even if uglified/minified, this will work since SomeObject will
// universally be changed to something like 'n'.
if (obj instanceof SomeObject) {}

(BTW, I need higher reputation to comment on the other worthy answers here)

Solution 3 - Javascript

Idea stolen from http://phpjs.org/functions/get_class/, posted by SeanJA. Ripped down to work with objects only and without need for a regular expression:

function GetInstanceType(obj)
{
	var str = obj.constructor.toString();
	return str.substring(9, str.indexOf("("));
}

function Foo() {
	this.abc = 123;
}

// will print "Foo"
GetInstanceType(new Foo());

I just learned an easier way to extract the function name from the constructor:

obj.constructor.name

Solution 4 - Javascript

You could also take a look at the way that they do it in php.js:

http://phpjs.org/functions/get_class:409

Solution 5 - Javascript

While instanceof is a correct answer it sure is ugly syntax. I offer that if you are creating custom objects you can add your own property for type and check against that like so...

var Car = function(){
    this.type = Object.defineProperty(this, "type", {value:"Car"});
}

This would create an immutable property called type that lives with the object. If you were using the Class syntax you could make it static as well.

... somewhere later ...

function addCar(car){
    if (car.type != "Car"){
        throw Error("invalid type for car");
    }

...

I think this solution is easy to implement, more intuitive, and thus easier for others to use and maintain.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBjartNView Question on Stackoverflow
Solution 1 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavascriptAAronView Answer on Stackoverflow
Solution 3 - JavascriptkungfoomanView Answer on Stackoverflow
Solution 4 - JavascriptSeanJAView Answer on Stackoverflow
Solution 5 - JavascriptcodeasaurusView Answer on Stackoverflow