Created
August 10, 2011 23:22
-
-
Save shawndumas/1138561 to your computer and use it in GitHub Desktop.
Surefire Type Detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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