js类型检测

1、检测字符串(string)、数值(number)、布尔值(boolean)、未定义(undefined)、函数(function) 、对象(object)使用typeof(在Safari和Chrome中检测正则也会返回 "function")

2、检测null 应用“===”,返回"null"

3、检测其它对象:

      方法一:利用instanceof/constructor  

      (再某些ie版本中存在跨iframe问题,每个iframe下都有自己的一套原型链,跨frame实例化的对象彼此是不共享原型链)

1 var isObject=value instanceof Object;
2 var isArray=value instanceof Array;//此处要想返回true,首先value必须是一个数组,而且必须与Array构造函数在同个全局作用域中。(Array是window的属性),如果value是在另一个frame中定义的数组,那么以上的代码会返回false.
3 var isDate=value instanceof Date;
4 var isRegExp=value instanceof RegExp;
5 var isError=value instanceof Error;
6 var isArray=(value.constructor == Array);

  方法二:利用 Object.prototype.toString.call()   (解决了方法一跨iframe 失效的问题)

 1 Object.prototype.toString.call ({});          //"[object Object]"
 2 Object.prototype.toString.call ([1,2,3,4]);   //"[object Array]"
 3 Object.prototype.toString.call(new Date());   //"[object Date]"
 4 Object.prototype.toString.call(/^hello/);     //"[object RegExp]"
 5 Object.prototype.toString.call(new Error());  //"[object Error]"
 6 Object.prototype.toString.call(new Number()); //"[object Number]"不建议用此方法创建变量
 7 Object.prototype.toString.call(123);          //"[object Number]"
 8 Object.prototype.toString.call(new String()); //"[object String]"不建议用此方法创建变量
 9 Object.prototype.toString.call('abc');        //"[object String]"
10 Object.prototype.toString.call(new Boolean());//"[object Boolean]"不建议用此方法创建变量
11 Object.prototype.toString.call(true);         //"[object Boolean]"

综合typeof和instanceof封装一个获取类型的函数:

 1 function type(ele) {
 2     var r;
 3     if (ele === null) r = 'null';
 4     else if (ele instanceof Array) r = 'array';
 5     else if (ele === window) r = 'window';
 6     else if (ele instanceof Date) r = 'date';
 7     else if (ele instanceof RegExp) r = 'regExp';
 8     else if (ele instanceof Function) r = 'function';
 9     else if (ele instanceof Error) r = 'Error';
10     else r = typeof ele;
11     return r;
12 }

参考jquery解决方案:

 1 function type(obj) {
 2     var class2type = {};
 3     var toString = class2type.toString;
 4     var arry = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
 5     arry.forEach(function(item, index) {
 6         class2type["[object " + item + "]"] = item.toLowerCase();
 7     });
 8     if (obj === null) {
 9         return obj + "";
10     }
11     return typeof obj === "object" || typeof obj === "function" ?
12         class2type[toString.call(obj)] || "object" :
13         typeof obj;
14 }

或者:

1 function getType(obj) {
2     var typeName;
3     return ((typeName = typeof(obj)) == "object" ? obj == null && "null" || Object.prototype.toString.call(obj).slice(8, -1):typeName).toLowerCase();
4 }

另外,ECMAScript 5 定义了一个新方法Array.isArray(),该函数在参数为数组时返回true,例如

       Array.isArray([1,2,3])     // true

       所以我们可以定义:  

 1 if(typeof Function.isFunction=="undefined"){
 2    Function.isFunction=function(value){
 3        return Object.prototype.toString.call(value)==="[object Function]";
 4    };
 5 }
 6 if(typeof Array.isArray=="undefined"){
 7    Array.isArray=function(value){
 8        return Object.prototype.toString.call(value)==="[object Array]";
 9    };
10 }
11 if(typeof Date.isDate=="undefined"){
12    Date.isDate=function(value){
13        return Object.prototype.toString.call(value)==="[object Date]";
14    };
15 }
16 if(typeof RegExp.isRegExp=="undefined"){
17    RegExp.isRegExp=function(value){
18        return Object.prototype.toString.call(value)==="[object RegExp]";
19    };
20 }
21 if(typeof Error.isError=="undefined"){
22    Error.isError=function(value){
23        return Object.prototype.toString.call(value)==="[object Error]";
24    };
25 }
26 if(typeof Object.isObject=="undefined"){
27    Object.isObject=function(value){
28        return Object.prototype.toString.call(value)==="[object Object]";
29    };
30 }
31 //......
原文地址:https://www.cnblogs.com/Iona/p/4724280.html