Skip to content

Instantly share code, notes, and snippets.

@leifoolsen
Created March 24, 2015 13:32
Show Gist options
  • Save leifoolsen/e1d3b75629c4969b5e4b to your computer and use it in GitHub Desktop.
Save leifoolsen/e1d3b75629c4969b5e4b to your computer and use it in GitHub Desktop.
Functions for working with type detection.
['Undefined', 'Null', 'Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Array', 'Object'].forEach(
function(name) {
var is = 'is' + name;
if(!window.is)
window[is] = function(obj) {
return ({}).toString.call(obj) === '[object ' + name + ']';
};
});
(function() {
var a;
function x() { return isArguments(arguments) };
console.debug("isUndefined(a) : " + isUndefined(a));
console.debug("isNull(null) : " + isNull(null));
console.debug("isFunction(x) : " + isFunction(x));
console.debug("isString('A string') : " + isString("A string"));
console.debug("isNumber(1) : " + isNumber(1));
console.debug("isNumber(1.1) : " + isNumber(1.1));
console.debug("isDate(new Date()) : " + isDate(new Date()));
console.debug("isRegExp(/[a..z]/i) : " + isRegExp(/[a..z]/i));
console.debug("isArray([1,2,3]) : " + isArray([1,2,3]));
console.debug("isObject({a:1,b:2,c:3}) : " + isObject({a:1,b:2,c:3}));
console.debug("isObject(new Object()) : " + isObject(new Object()));
console.debug("isArguments(arguments) : " + isArguments(arguments));
console.debug("isArguments x(1, 2, {a:4, b:5}) : " + x(1, 2, {a:4, b:5}));
console.debug("isArguments x({a:4, b:5}) : " + x({a:4, b:5}));
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment