1.判断变量是否为数组的数据类型?

  方法一 :判断其是否具有“数组性质”,如slice()方法。可自己给该变量定义slice方法,故有时会失效。

  方法二 :obj instanceof Array ,但是在某些IE版本中不正确

  方法三 :来自于zepto.js源码 , 

function isArray(arg){ 
    return Object.prototype.toString.call(arg) === "[object Array]"    
}

         对于支持ECMA Script5中的新方法Array.isArray()的浏览器可以做如下优化。

if(typeof Array.isArray==="undefined")
{
  Array.isArray = function(arg){
        return Object.prototype.toString.call(arg)==="[object Array]"
    };  
}

  2.判断变量是否为对象

function isObject(arg){ 
    return Object.prototype.toString.call(arg) === "[object Object]"    
}

  更加深层次的判断

function isPlainObject(obj) {
	return isObject(obj)  && Object.getPrototypeOf(obj) == Object.prototype
}

  3.判断变量是否为函数

function isFunction(arg){ 
    return Object.prototype.toString.call(arg) === "[object Function]"    
}

  

  

  

相关文章:

  • 2021-12-02
  • 2021-11-18
  • 2022-12-23
  • 2022-01-04
  • 2021-12-23
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2021-05-19
  • 2021-05-26
  • 2022-02-03
  • 2021-12-25
  • 2021-07-10
  • 2022-02-11
  • 2021-11-13
相关资源
相似解决方案