In jQuery è spesso necessario reperire il tipo di dati in uso.
Possiamo implementare la seguente funzione di utility:
$.getType = function ( value ) {
var match;
var key;
var cons;
var types;
var type = typeof value;
if ( type === "object" && !value ) {
return 'null';
}
if ( type === "object" ) {
if (!value.constructor) {
return "object";
}
cons = value.constructor.toString();
match = cons.match( /(\w+)\(/ );
if ( match ) {
cons = match[1].toLowerCase();
}
types = [ "boolean", "number", "string", "array" ];
for ( key in types ) {
if ( cons === types[key] ) {
type = types[key];
break;
}
}
}
return type;
};