方法一
Array.isArray(object)
1 var arr = []; 2 console.log(Array.isArray(arr));//true
方法二
Object.prototype.toString.call(object) == [object object]
1 //检测各种数据类型的方法 2 var is = { types : ["Array", "Boolean", "Date", "Number", "Object", "RegExp", "String", "Window", "HTMLDocument"] } ; 3 4 for(var i=0,c;c=is.types[i++];){ 5 is[c]=(function(type){ 6 return function(obj){ 7 return Object.prototype.toString.call(obj) == "[object" + type + "]"; 8 } 9 })(c); 10 } 11 12 var arr = [1,2,3]; 13 var str = ‘123‘; 14 var num = 123; 15 var a = true; 16 var b = new Date(); 17 var obj = new Object(); 18 var c = new RegExp(/0-9/ig); 19 20 console.log(is.Array(arr));//true 21 console.log(is.String(str));//true 22 console.log(is.Number(num));//true 23 console.log(is.Boolean(a));//true 24 console.log(is.Date(b));//true 25 console.log(is.Object());//true 26 console.log(is.RegExp(c));//true 27 console.log(is.Window(window));//true 28 console.log(is.HTMLDocument(document));//true
此方法可以检测所有的数据类型
时间: 2024-10-13 10:48:28