Any developer writing JS sooner or later encounters this, rather infuriating behaviour:
typeof []; // 'object'
While there are workarounds such as instanceof
and accessing a variable's .constructor
property, this just came to my mind:
Array.prototype.TYPE = 'Array';
String.prototype.TYPE = 'String';
Boolean.prototype.TYPE = 'Boolean';
Object.prototype.TYPE = 'Object';
RegExp.prototype.TYPE = 'RegExp';
Number.prototype.TYPE = 'Number';
// and therefore:
[1,2,'bar', {baz: 'foo'}].TYPE === 'Array'; // true
"But life itself, my wife, and all the world / Are not with me esteemed above thy life".TYPE // 'String'
(42).TYPE === 'Number'; // true
// and so on
I know it is generally frowned upon to modify native prototypes, That said, is there any other issue with this "pattern"?
UPDATE:
Some comments offer alternative solutions like usage of Object.prototype.toString
and more. These are certainly all valid approaches that have their use cases. But still, what I'm primarily interestest in, is whether there might be some case in which adding a property to native constructor prototypes would actually cause a problem :)
UPDATE:
A safer way?
Array.prototype.getType = function() {return 'Array'};
String.prototype.getType = function() {return 'String'};
Boolean.prototype.getType = function() {return 'Boolean'};
Object.prototype.getType = function() {return 'Object'};
RegExp.prototype.getType = function() {return 'RegExp'};
Number.prototype.getType = function() {return 'Number'};