Skip to content

Instantly share code, notes, and snippets.

@klaascuvelier
Last active December 23, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klaascuvelier/6607961 to your computer and use it in GitHub Desktop.
Save klaascuvelier/6607961 to your computer and use it in GitHub Desktop.
Type check for JavaScript objects. Based on code from @jonbretman's slides from JsConf (https://speakerdeck.com/jonbretman/ask-not-what-javascript-can-do-for-you, slide 25) Fix for non-isNaN types.
var toString = Object.prototype.toString,
regex = /\[object (.*?)\]/,
type = function (o) {
var match, typeMatch;
// Special case for DOM elements
if (o && o.nodeType === 1) {
return 'element';
}
match = toString.call(o).match(regex);
typeMatch = match[1].toLowerCase();
// Special case for NaN
if (typeMatch === 'number') {
return isNaN(o) ? 'nan' : typeMatch;
} else {
return typeMatch;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment