Last active
October 12, 2015 18:31
-
-
Save sarfraznawaz2005/7557724b65f68219b876 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
// We know typeof has problms, here is a better solution | |
Object.toType = function(obj) { | |
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
} | |
// Examples: | |
Object.toType(window); //"global" (all browsers) | |
Object.toType([1,2,3]); //"array" (all browsers) | |
Object.toType(/a-z/); //"regexp" (all browsers) | |
Object.toType(JSON); //"json" (all browsers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment