日期:2014-05-16  浏览次数:20319 次

关于函数isArraylike如何理解,求助。
JQ里的一段代码
function isArraylike( obj ) {
var length = obj.length,
type = jQuery.type( obj );

if ( jQuery.isWindow( obj ) ) {
return false;
}

if ( obj.nodeType === 1 && length ) {
return true;
}

return type === "array" || type !== "function" &&
( length === 0 ||
typeof length === "number" && length > 0 && ( length - 1 ) in obj );
}

关于最后这个return,这里的in不知道是什么意思,我只知道for...in,in单独用是什么意思

使用(||)这个符号,我想知道就算返回了,这个return是把他后面一大段都返回了,

还是有选择的返回,如果有选择的返回,它的判断条件在哪里?

返回一个类型为数组或者类型不是function并且

这里的括号里东西该如何理解。

------解决方案--------------------

return type === "array" 
------解决方案--------------------
 type !== "function" &&
        ( length === 0 
------解决方案--------------------

        typeof length === "number" && length > 0 && ( length - 1 ) in obj );
//若type==='array'直接返回true
//若type!=='array'的话,如果type!=='function'为true的话开始判断括号里的内容,否则整体返回false
//括号里的内容如果length===0为true若括号里整体为true,整体返回true
//若length===0为false,判断typeof length==='number',如果为flase,整体返回false
//如果typeof length==='number',如果为true,判断length>0,如果为false,整体返回false
//如果length>0为true,判断( length - 1 ) in obj,这话的意思就是如果是类数组的对象,
其结构肯定是{0:'aaa',1:'bbb',length:2}这样的key值为数字的,所以如果是类数组对象,判断在obj里是否能找到length-1这样的key,如果找到,整体返回true,否则整体返回false
//in就是判断一个key是否在一个obj里。比如var obj = {a:'111'},'a' in obj为true,'b' in obj为false