Created
January 5, 2018 09:45
-
-
Save petermeissner/8562eabecd0b5b69c36c89a189b1bfc0 to your computer and use it in GitHub Desktop.
checking for specific type / class in Javascript
This file contains hidden or 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
// - 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