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也不完美,自定义的东西随意性太大了,暂时没办法一网打尽……以后慢慢改进!
下面是测试: