Created
September 9, 2015 14:31
-
-
Save leolanese/36fe3879dd7b97c9f63f to your computer and use it in GitHub Desktop.
a betterTypeOf
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 betterTypeOf = function(obj) { | |
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase() | |
} | |
/* | |
betterTypeOf(null); // null | |
betterTypeOf(NaN); // number | |
betterTypeOf({a: 4}); //"object" | |
betterTypeOf([1, 2, 3]); //"array" | |
(function() {console.log(toType(arguments))})(); //arguments | |
betterTypeOf(new ReferenceError); //"error" | |
betterTypeOf(new Date); //"date" | |
betterTypeOf(/a-z/); //"regexp" | |
betterTypeOf(Math); //"math" | |
betterTypeOf(JSON); //"json" | |
betterTypeOf(new Number(4)); //"number" | |
betterTypeOf(new String("abc")); //"string" | |
betterTypeOf(new Boolean(true)); //"boolean" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment