zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    053587
    053586
    053585
    053584
    053583
    053582
    053581
    053580
    053579
    053578
  • 原文地址:https://www.cnblogs.com/EnSnail/p/6913127.html
Copyright © 2011-2022 走看看