Skip to content

Instantly share code, notes, and snippets.

@afahy
Created December 28, 2011 20:03
Show Gist options
  • Save afahy/1529456 to your computer and use it in GitHub Desktop.
Save afahy/1529456 to your computer and use it in GitHub Desktop.
Simple testing for variable types
// 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