Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle sparse arrays properly in `Array#_each` to match behavior with…

… browsers' built-in `Array#forEach` (and ES5). [#790 state:resolved] (Andriy Tyurnikov, Yaffle, Andrew Dupont)
  • Loading branch information...
commit 5dc12bd4316f2b7f64e8fe753e14d8db867eda75 1 parent 1a5f049
@savetheclocktower savetheclocktower authored
Showing with 14 additions and 5 deletions.
  1. +2 −0  CHANGELOG
  2. +5 −4 src/lang/array.js
  3. +7 −1 test/unit/array_test.js
View
2  CHANGELOG
@@ -1,3 +1,5 @@
+* Handle sparse arrays properly in `Array#_each` to match behavior with browsers' built-in `Array#forEach` (and ES5). [#790 state:resolved] (Andriy Tyurnikov, Yaffle, Andrew Dupont)
+
* Make `Event.extend` work with legacy IE events in IE 9. (Andrew Dupont)
* Stop appending `&_=` to the parameters for non-GET Ajax requests in Safari. We no longer support any version of Safari for which this is necessary. [#327 state:resolved] (John-David Dalton, Andrew Dupont)
View
9 src/lang/array.js
@@ -189,12 +189,13 @@ Array.from = $A;
slice = arrayProto.slice,
_each = arrayProto.forEach; // use native browser JS 1.6 implementation if available
- function each(iterator) {
- for (var i = 0, length = this.length; i < length; i++)
- iterator(this[i]);
+ function each(iterator, context) {
+ for (var i = 0, length = this.length >>> 0; i < length; i++) {
+ if (i in this) iterator.call(context, this[i], i, this);
+ }
}
if (!_each) _each = each;
-
+
/**
* Array#clear() -> Array
*
View
8 test/unit/array_test.js
@@ -42,7 +42,7 @@ new Test.Unit.Runner({
this.assertEnumEqual([], $A(5));
this.assertEnumEqual([], $A(true));
},
-
+
testClear: function(){
this.assertEnumEqual([], [].clear());
this.assertEnumEqual([], [1].clear());
@@ -185,5 +185,11 @@ new Test.Unit.Runner({
testConcat: function(){
var args = (function() { return [].concat(arguments) })(1, 2);
this.assertIdentical(1, args[0][0]);
+ },
+
+ testEachOnSparseArrays: function() {
+ var sparseArray = [0, 1];
+ sparseArray[5] = 5;
+ this.assertEqual('[0, 1, 5]', sparseArray.inspect(), "Array#each should skip nonexistent keys in an array");
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.