// args is for internal usage only\r
each: function( object, callback, args ) {\r
if ( args ) {\r
- if ( object.length == undefined )\r
+ if ( object.length == undefined ) {\r
for ( var name in object )\r
- callback.apply( object[ name ], args );\r
- else\r
+ if ( callback.apply( object[ name ], args ) === false )\r
+ break;\r
+ } else\r
for ( var i = 0, length = object.length; i < length; i++ )\r
if ( callback.apply( object[ i ], args ) === false )\r
break;\r
\r
// A special, fast, case for the most common use of each\r
} else {\r
- if ( object.length == undefined )\r
+ if ( object.length == undefined ) {\r
for ( var name in object )\r
- callback.call( object[ name ], name, object[ name ] );\r
- else\r
+ if ( callback.call( object[ name ], name, object[ name ] ) === false )\r
+ break;\r
+ } else\r
for ( var i = 0, length = object.length, value = object[0]; \r
i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}\r
}\r
});
test("$.each(Object,Function)", function() {
- expect(8);
+ expect(12);
$.each( [0,1,2], function(i, n){
ok( i == n, "Check array iteration" );
});
$.each( { name: "name", lang: "lang" }, function(i, n){
ok( i == n, "Check object iteration" );
});
+
+ var total = 0;
+ jQuery.each([1,2,3], function(i,v){ total += v; });
+ ok( total == 6, "Looping over an array" );
+ total = 0;
+ jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
+ ok( total == 3, "Looping over an array, with break" );
+ total = 0;
+ jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
+ ok( total == 6, "Looping over an object" );
+ total = 0;
+ jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
+ ok( total == 3, "Looping over an object, with break" );
});
test("$.prop", function() {