Created
December 28, 2011 20:03
-
-
Save afahy/1529456 to your computer and use it in GitHub Desktop.
Simple testing for variable types
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
// Can substitute "this" ( / "global" ) for whatever object you want to extend, eg "library.util" or whatever | |
// | |
// Creates: | |
// | |
// isBoolean( obj ) | |
// isNumber( obj ) | |
// isString( obj ) | |
// isFunction( obj ) | |
// isArray( obj ) | |
// isDate( obj ) | |
// isRegExp( obj ) | |
// isObject( obj ) | |
( function( global ) { | |
var types = 'Boolean Number String Function Array Date RegExp Object'.split(' '), | |
toStr = Object.prototype.toString, | |
i = 0; | |
while ( i < types.length ) { | |
var type = types[ i ], | |
test = '[object ' + type + ']'; | |
global['is' + type ] = ( function( type, test ) { | |
return function( obj ) { | |
return toStr.call( obj ) === test; | |
}; | |
} )( type, test ); | |
i++; | |
} | |
}( this ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment