Difference in Jquery.each() and Array.prototype.forEach() method
Placement of arguments in the callback.
Quantity of arguments in the callback (The
.forEach()
gives you get a reference to the original collection.)Default
this
value in the callback. (In jQuery it's the current item, in.forEach()
it's the JavaScript default)Ability to manually set the
this
value in the callback. (jQuery doesn't give this option,.forEach()
lets you via a third argument.)Avoidance of non-defined properties on sparse Arrays. (The
.forEach()
avoids them, jQuery includes them.)
They're very differently behaving methods. jQuery's doesn't make any attempt to be compliant with or complimentary of the standard behaviors.
In addition to Crazy Train's answer:
What i wan to know is ,do jquery.each() invokes function for object without length property??
Read the source:
// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0,
length = object.length,
isObj = length === undefined || jQuery.isFunction( object );
if ( args ) {
...
// A special, fast, case for the most common use of each
} else {
if ( isObj ) {
for ( name in object ) {
}
...
}
}
return object;
},
So you can see if there is no length property, or it has the value undefined, then jQuery thinks it's a plain object and will do a for..in loop over the enumerable properties with no guard against inherited properties.