Given this:
function SomeType () { return this; }
How am I able to check an object's type with only a String?
It's all good if I have a reference to the constructor, like so:
new SomeType() instanceof SomeType; // true
But if I want to check the type as a String, there is no easy way to check.
new SomeType() instanceof 'SomeType'; // TypeError
I could inspect the constructor after it is converted to a String:
function SomeType () { return this; }
/function \bSomeType\b/.test( String(new SomeType().constructor) ); // true
But it doesn't work in all scenarios:
var SomeType = function () { return this; }
/function \bSomeType\b/.test( String(new SomeType().constructor) ); // false
Any thoughts on this? Would attempting to validate a type/constructor by-way-of a String be considered an anti-pattern?
var SomeType = function () { … };; that constructor doesn’t have a name, so you can’t check it.