console.log(typeof function(){return;});//function
console.log(typeof "a");//string
console.log(typeof 123);//number
console.log(typeof a);//undefined
console.log(typeof true);// boolean
console.log(typeof NaN);//number
console.log(typeof !NaN);//boolean
console.log(typeof {name:'大漠',age:'37'});//object
console.log(typeof ['大漠','37']);// object
console.log(typeof null);//object
上面的代码示例告诉我们,typeof {name:"大漠",age: "37"}typeof ["大漠","37"]返回的都是object。事实再次证明typeof没办法对数组进行检测,那么这里引出一个问题?如何判断数据是个数组类型?
最佳检测方法:
var isArray = (function(){
   if(Array.isArray){
     return Array.isArray;
}
var objectToStringFn = Object.prototype.toString,
   arrayToStringResult = objextToStringFn.call([]);
 return function(subject){
   return objectToStringFn.call(subject)===arrayToStringResult;
};
}());
var arr = [];
isArray(arr);//true
最优化的方法就是不管Array.isArray'是否能用,都可以回到对象原生toString检测和对象原生toString`检测上。
10-20 18:01