js中判断一个变量是否为类数组的方法
2019-08-13 本文已影响0人
大兵_HERG
1.判断类数组的方法封装
function isArrayLike( obj ) {
var length = !!obj && "length" in obj && obj.length,
//类型判断
type = typeFn( obj );
if ( isFunction( obj ) || isWindow( obj ) ) {
return false;
}
return type === "array" || length === 0 ||
typeof length === "number" && length > 0 && ( length - 1 ) in obj;
}
//上述用到的判断方法正是封装的方法,用起来很方便