[jQuery] expand jQuery.each() to handle name/index "with args" in callback function.
/
*********************************************************************************************************************
*
* the extened function
*
**********************************************************************************************************************/
// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0, length = object.length;
if ( args ) {
args.unshift("","") ; // inert placeholder
if ( length == undefined ) {
for ( name in object ){
args[0] = name; // load the actual parameter
args[1] = object[ name ]; // load the actual parameter
if ( callback.apply( object[ name ], args ) === false )
break;
}
} else
for ( ; i < length; ){
args[0] = i; // load the actual parameter
args[1] = object[ i ]; // load the actual parameter
if ( callback.apply( object[ i++ ], args ) === false )
break;
}
// A special, fast, case for the most common use of each
} else {
if ( length == undefined ) {
for ( name in object )
if ( callback.call( object[ name ], name, object[ name ] ) ===
false )
break;
} else
for ( var value = object[0];
i < length && callback.call( value, i, value ) !== false; value =
object[++i] ){}
}
return object;
},
/
*********************************************************************************************************************
*
* the original function
*
**********************************************************************************************************************/
// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0, length = object.length;
if ( args ) {
if ( length == undefined ) {
for ( name in object )
if ( callback.apply( object[ name ], args ) === false )
break;
} else
for ( ; i < length; )
if ( callback.apply( object[ i++ ], args ) === false )
break;
// A special, fast, case for the most common use of each
} else {
if ( length == undefined ) {
for ( name in object )
if ( callback.call( object[ name ], name, object[ name ] ) ===
false )
break;
} else
for ( var value = object[0];
i < length && callback.call( value, i, value ) !== false; value =
object[++i] ){}
}
return object;
},
/
*********************************************************************************************************************/