Created
March 4, 2021 09:55
-
-
Save DesignFront/fbaf2e78cf216dec8aa8cf11f5433091 to your computer and use it in GitHub Desktop.
better typeof
This file contains hidden or 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
// toType better typeof | |
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(new Number(4)); //"number" | |
toType(new String("abc")); //"string" | |
toType(new Boolean(true)); //"boolean" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment