js 判断 obj 是否是 数组 array

  参考文章: http://www.kuitao8.com/20140511/2418.shtml

function objType(obj) {
    //var type = Object.prototype.toString.call(obj); 
    // 简写
    var type = toString.call(obj); 
    // array:type = [object Array] 
    // obj:  type = [object object]
    // arguments: type = [object arguments]
    // string: type = [object String]
    // number: type = [object Number]
    // boolean: type = [object Boolean]
    // function: type = [object Function]
    // undefined: type = [object Undefined]
    return type ? type.substring(8,type.length-1) : '';
}

function isArray(obj){
    var type = objType(obj);
    return type && type === 'Array' ? true : false;
}
console.log(objType([])); // Array
console.log(objType({})); // Object
console.log(isArray({})); // false
console.log(isArray([])); // true

 

posted @ 2017-05-27 15:35  ESnail  阅读(965)  评论(0编辑  收藏  举报