[SOLVED] Value type checking

Hello,

I am wondering if it is possible to detect what type of the object is passed in build mode. If I use:

passedValue.constructor.name.match('Entity') <--- Editor mode works fine (returns 'Entity' value)
passedValue.constructor.name.match('Entity') <--- Build mode crashes (returns 'e' (which is undefined))

How to check the type of data in build mode, since if I do:

typeof passedValue <--- returns 'Object' for a lot of things (Vectors, Arrays, Functions, Classes, etc.).

I wanted to be more specific and check for specific value, like Entity or ScriptComponent. How can I achieve that?

Try instanceof instead: instanceof - JavaScript | MDN

eg

if (passedValue instanceof pc.Entity) {
 console.log('I\'m an entity');
}
1 Like

Nice, that works perfectly.

Thank you very much!

*Edit: This works well for everything that is defined by ‘new’ object.

var a = new Boolean(false) 
console.log(a instanceof Boolean) <--- returns true

var b = false;
console.log(b instanceof Boolean) <--- returns false

So, use only with defined values (values that use ‘new’ keyword).