Skip to content

Instantly share code, notes, and snippets.

@sarfraznawaz2005
Last active October 12, 2015 18:31
Show Gist options
  • Save sarfraznawaz2005/7557724b65f68219b876 to your computer and use it in GitHub Desktop.
Save sarfraznawaz2005/7557724b65f68219b876 to your computer and use it in GitHub Desktop.
Better typeof
// 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