Skip to content

Instantly share code, notes, and snippets.

@petermeissner
Created January 5, 2018 09:45
Show Gist options
  • Save petermeissner/8562eabecd0b5b69c36c89a189b1bfc0 to your computer and use it in GitHub Desktop.
Save petermeissner/8562eabecd0b5b69c36c89a189b1bfc0 to your computer and use it in GitHub Desktop.
checking for specific type / class in Javascript
// - check type of input
is_type_x = function(object, type){
// go through cases: undefined, object, primitive
if( typeof(object) === "undefined" ){
// undefined cannot be matched
return false;
} else if( typeof(object) === "object" ){
// objects have to be matched against constructor
if( window[type] === undefined ){
return false;
}else{
return ( object instanceof eval(type) );
}
} else if(
// everything not an object has to match againt its type
$.inArray(
typeof(object),
["number", "boolean", "string", "symbol", "function"]
) >= 0
){
if ( typeof(object) === type ){
return true;
}else{
return false;
}
}
// if those checks do not catch, return false
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment