Skip to content

Instantly share code, notes, and snippets.

@shawndumas
Created August 10, 2011 23:22
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 shawndumas/1138561 to your computer and use it in GitHub Desktop.
Save shawndumas/1138561 to your computer and use it in GitHub Desktop.
Surefire Type Detection
var typs = {};
(function (self) {
var values = [
'Function',
'Object',
'Array',
'String',
'Number',
'Date',
'RegExp',
'Boolean',
'Null',
'Error'
];
for (var i in values) if (values.hasOwnProperty(i)) {
var value = values[i];
self['is' + value] = (function (v) {
return function (o) {
var r = '';
try {
r = (o === null) ?
'Null' :
Object
.prototype
.toString
.call(o)
.replace(/^\[object\s(\w+)\]$/, '$1');
} catch (e) {
r = 'Undefined';
}
return !!(r === v);
};
})(value);
}
})(typs);
alert(typs.isFunction(Object)); // --> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment