/ Published in: JavaScript
Helps identify correctly the type of whatever you put as an argument. This was adapted from http://www.planetpdf.com/developer/article.asp?ContentID=testing_for_object_types_in_ja
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var is={ Null:function(a){ return a===null; }, Undefined:function(a){ return a===undefined; }, nt:function(a){ return(a===null||a===undefined); }, Function:function(a){ return(typeof(a)==='function')?a.constructor.toString().match(/Function/)!==null:false; }, String:function(a){ return(typeof(a)==='string')?true:(typeof(a)==='object')?a.constructor.toString().match(/string/i)!==null:false; }, Array:function(a){ return(typeof(a)==='object')?a.constructor.toString().match(/array/i)!==null||a.length!==undefined:false; }, Boolean:function(a){ return(typeof(a)==='boolean')?true:(typeof(a)==='object')?a.constructor.toString().match(/boolean/i)!==null:false; }, Date:function(a){ return(typeof(a)==='date')?true:(typeof(a)==='object')?a.constructor.toString().match(/date/i)!==null:false; }, HTML:function(a){ return(typeof(a)==='object')?a.constructor.toString().match(/html/i)!==null:false; }, Number:function(a){ return(typeof(a)==='number')?true:(typeof(a)==='object')?a.constructor.toString().match(/Number/)!==null:false; }, Object:function(a){ return(typeof(a)==='object')?a.constructor.toString().match(/object/i)!==null:false; }, RegExp:function(a){ return(typeof(a)==='function')?a.constructor.toString().match(/regexp/i)!==null:false; } }; var type={ of:function(a){ for(var i in is){ if(is[i](a)){ return i.toLowerCase(); } } } };
URL: http://rolandog.com/archives/2007/01/18/typeof-a-more-specific-typeof/