Reputation: 587
There is a function named 'isArrayLike' in jquery which used by many functions for example $.each
function isArraylike( obj ) {
var length = obj.length,
type = jQuery.type( obj );
if ( type === "function" || jQuery.isWindow( obj ) ) {
return false;
}
if ( obj.nodeType === 1 && length ) {
return true;
}
return type === "array" || length === 0 ||
typeof length === "number" && length > 0 && ( length - 1 ) in obj;
}
I know it is used to see whether it is like an array,but i do not know why the second if.
It checks nodeType to make sure it's an element and why length?Is an element have length property?
Thx
Upvotes: 1
Views: 376
Reputation: 6698
If you find undocumented code that doesn't make sense, one thing you can do is look at why someone wrote it in the first place. Here's the commit that adds the nodeType
check:
https://github.com/jquery/jquery/commit/3c7f2af81d877b24a5e5b6488e78621fcf96b265
Judging by the test that they added with it, it's to support form
elements, which it will treat as an array of controls.
Upvotes: 2