jquey源码—— isArrayLike 判断是否为数组,类数组;

源码:


function isArrayLike( obj ) {
    //  是否存在length 属性
    var length = !!obj && "length" in obj && obj.length,
        type = jQuery.type( obj );
    // 排除function window 
    if ( type === "function" || jQuery.isWindow( obj ) ) {
        return false;
    }
    // 数组判断: type === "array"  类数组判断:存在length属性值; length值为数字且大于0; obj[length - 1] 存在; 
    return type === "array" || length === 0 ||
        typeof length === "number" && length > 0 && ( length - 1 ) in obj;
}

类数组判断条件: 1、存在length 属性值,且length 值为大于0 的数;2、存在obj[length - 1];

猜你喜欢

转载自blog.csdn.net/weixin_41265663/article/details/82720191