typeof 220; // "number"
typeof "220"; // "string"
typeof false; // "boolean"
typeof [1, 2, 3]; // "object"
typeof a; // "undefined"
typeof Symbol(); // "symbol"
typeof console.log; // "function"
類(lèi)型操作符typeof返回值有7種:number,string,boolean腰奋,object,undefined抱怔,symbol和function劣坊。
可以發(fā)現(xiàn)用typeof只能測(cè)出arr是一個(gè)對(duì)象,因?yàn)閠ypeof7種返回之中沒(méi)有array屈留,所以要判斷是否是一個(gè)數(shù)組局冰,可以有如下4種方式:
1、instanceof
arr instanceof Array; //true
2灌危、isArray
Array.isArray(arr); //true
3康二、constructor
arr.constructor === Array; //true
4、由于在iframe中創(chuàng)建的Array并不共享prototype勇蝙,此時(shí)可以:
function isArray(obj){
return Object.prototype.toString.call(obj) === '[object Array]';
}
isArray(arr); // true