Skip to content

Commit

Permalink
In {Set,Map}#forEach and Array#find{,Index}, don't use Function#c…
Browse files Browse the repository at this point in the history
…all unless a context is provided.
  • Loading branch information
ljharb committed Nov 20, 2014
1 parent c955ed7 commit bfade61
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,14 @@
if (!ES.IsCallable(predicate)) {
throw new TypeError('Array#find: predicate must be a function');
}
var thisArg = arguments[1];
var thisArg = arguments.length > 1 ? arguments[1] : null;
for (var i = 0, value; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) { return value; }
if (thisArg) {
if (predicate.call(thisArg, value, i, list)) { return value; }
} else {
if (predicate(value, i, list)) { return value; }
}
}
return;
},
Expand All @@ -776,9 +780,13 @@
if (!ES.IsCallable(predicate)) {
throw new TypeError('Array#findIndex: predicate must be a function');
}
var thisArg = arguments[1];
var thisArg = arguments.length > 1 ? arguments[1] : null;
for (var i = 0; i < length; i++) {
if (predicate.call(thisArg, list[i], i, list)) { return i; }
if (thisArg) {
if (predicate.call(thisArg, list[i], i, list)) { return i; }
} else {
if (predicate(list[i], i, list)) { return i; }
}
}
return -1;
},
Expand Down Expand Up @@ -1818,7 +1826,11 @@
var context = arguments.length > 1 ? arguments[1] : null;
var it = this.entries();
for (var entry = it.next(); !entry.done; entry = it.next()) {
callback.call(context, entry.value[1], entry.value[0], this);
if (context) {
callback.call(context, entry.value[1], entry.value[0], this);
} else {
callback(entry.value[1], entry.value[0], this);
}
}
}
});
Expand Down Expand Up @@ -1956,9 +1968,13 @@
forEach: function (callback) {
var context = arguments.length > 1 ? arguments[1] : null;
var entireSet = this;
ensureMap(this);
ensureMap(entireSet);
this['[[SetData]]'].forEach(function (value, key) {
callback.call(context, key, key, entireSet);
if (context) {
callback.call(context, key, key, entireSet);
} else {
callback(key, key, entireSet);
}
});
}
});
Expand Down

0 comments on commit bfade61

Please sign in to comment.