From c6b9654ea8524e76f49f17f1bb0f6a38ce36bca3 Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Sat, 20 Jun 2009 15:51:19 +0000 Subject: [PATCH] jquery core: Closes #2827. jQuery.each iterates over functions --- src/core.js | 8 +++++--- test/unit/core.js | 9 ++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 9d0d41f8b..78f48d71b 100644 --- a/src/core.js +++ b/src/core.js @@ -307,10 +307,12 @@ jQuery.extend({ // args is for internal usage only each: function( object, callback, args ) { - var name, i = 0, length = object.length; + var name, i = 0, + length = object.length, + isObj = length === undefined || jQuery.isFunction(object); if ( args ) { - if ( length === undefined ) { + if ( isObj ) { for ( name in object ) { if ( callback.apply( object[ name ], args ) === false ) { break; @@ -326,7 +328,7 @@ jQuery.extend({ // A special, fast, case for the most common use of each } else { - if ( length === undefined ) { + if ( isObj ) { for ( name in object ) { if ( callback.call( object[ name ], name, object[ name ] ) === false ) { break; diff --git a/test/unit/core.js b/test/unit/core.js index ade341527..ac93ddaa2 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -494,7 +494,7 @@ test("jQuery.extend(Object, Object)", function() { }); test("jQuery.each(Object,Function)", function() { - expect(12); + expect(13); jQuery.each( [0,1,2], function(i, n){ equals( i, n, "Check array iteration" ); }); @@ -519,6 +519,13 @@ test("jQuery.each(Object,Function)", function() { total = 0; jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; }); equals( total, 3, "Looping over an object, with break" ); + + var f = function(){}; + f.foo = 'bar'; + jQuery.each(f, function(i){ + f[i] = 'baz'; + }); + equals( "baz", f.foo, "Loop over a function" ); }); test("jQuery.makeArray", function(){ -- 2.39.5