jQuery的makeArray有其局限性(1.3.4还有bug),我自己实现了一个,不过涉及N多辅助方法。

      var dom = {},
      _toString = Object.prototype.toString,
      _slice = Array.prototype.slice;
      dom.is = function(obj,type) {
        return _toString.call(obj).match(/^\[object\s(.*)\]$/)[1] === type;
      }
      dom.isArray = function (obj) {
        return dom.is(obj,"Array");
      }
      dom.isNumber = function (obj) {
        return dom.is(obj,"Number");
      }
      dom.isString = function (obj) {
        return dom.is(obj,"String");
      }
      dom.isArrayLike =  function (obj) {//包括Array
        if(dom.isArray(obj) || obj.callee) return true;
        if(dom.is(obj,'NodeList')) return true;
        if(dom.is(obj,'HTMLCollection')) return true;
        //不能为字符串,不能为window,具有length属性
        if(dom.isNumber(obj.length) && !dom.isString(obj) && !obj.eval){
          if(obj.nextNode || obj.item)
            return true;
          var n = obj.length - 1 

早期尝试使用Ext.isIterable,不过它有点小BUG,如放一个函数进去,它会返回undefined,好歹返回false嘛,另对于用户自定义的类数组对象无法检测,残念!不过我的isArrayLike也不完美,自定义的东西随意性太大了,暂时没办法一网打尽……以后慢慢改进!

下面是测试:

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2021-10-24
  • 2021-12-12
  • 2021-11-20
  • 2022-12-23
  • 2021-11-20
  • 2021-11-12
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案