Last active
March 5, 2018 16:49
-
-
Save oropesa/b3251ac6c5a4a3f07a29124f8cf5378b to your computer and use it in GitHub Desktop.
'typeof' return de basic type. 'toType' return the specify type. EXAMPLE [object Date] return 'date', not 'object'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var toType = function(obj) { | |
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() | |
} | |
toType({a: 4}); //"object" | |
toType([1, 2, 3]); //"array" | |
(function() {console.log(toType(arguments))})(); //arguments | |
toType(new ReferenceError); //"error" | |
toType(new Date); //"date" | |
toType(/a-z/); //"regexp" | |
toType(Math); //"math" | |
toType(JSON); //"json" | |
toType(4); //"number" | |
toType(new Number(4)); //"number" | |
toType("abc"); //"string" | |
toType(new String("abc")); //"string" | |
toType(true); //"boolean" | |
toType(new Boolean(true)); //"boolean" | |
toType(function(){}); //"function" | |
toType(new function(){}); //"object" | |
typeof {a: 4}; //"object" | |
typeof [1, 2, 3]; //"object" | |
(function() {console.log(typeof arguments)})(); //object | |
typeof new ReferenceError; //"object" | |
typeof new Date; //"object" | |
typeof /a-z/; //"object" | |
typeof Math; //"object" | |
typeof JSON; //"object" | |
typeof 4; //"number" | |
typeof new Number(4); //"object" | |
typeof "abc"; //"string" | |
typeof new String("abc"); //"object" | |
typeof true; //"boolean" | |
typeof new Boolean(true); //"object" | |
typeof function(){}; //"function" | |
typeof new function(){}; //"object" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment