Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created January 17, 2013 05:42
Show Gist options
  • Save WebReflection/4553965 to your computer and use it in GitHub Desktop.
Save WebReflection/4553965 to your computer and use it in GitHub Desktop.
All JSON compatible (and incompatible thanks to "function") types in a simple cross platform function
var type = function (isArray, type){
return function(object) {
type = typeof object;
return type === "object" ? (
object ? (
isArray(object) ? "array" : type
) : "null"
) : type;
};
}(Array.isArray || function(Array, toString, array){
array = toString.call([]);
return function(object) {
return (
object instanceof Array ||
toString.call(object) === array
);
}
}(Array, {}.toString));
@WebReflection
Copy link
Author

to test above snippet:

alert([
  type(""),          // string
  type(1),           // number
  type({}),          // object
  type([]),          // array
  type(true),        // boolean
  type(false),       // boolean
  type(null),        // null
  type(),            // undefined
  type(function(){}) // function
].join("\n"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment