Permalink
Browse files

Set.each() & StringSet.each() behaves as Array.each()

  • Loading branch information...
1 parent f84e8d7 commit bf9212fc9a696f3996f744a9903a573e4231f03d @Havvy Havvy committed Feb 3, 2012
Showing with 13 additions and 3 deletions.
  1. +13 −3 lib/simplesets.js
View
@@ -129,9 +129,14 @@ var SetPrototype = {
// is changed by the callback, the results are undefined.
// Callback takes the same parameters as the forEach method of
// arrays: value, index, set
- each: function(callback) {
+ // Takes an optional parameter that sets what this is bound to.
+ each: function(callback, thisArg) {
// If there's no callback, don't bother.
if (!callback) return;
+
+ if (thisArg) {
+ callback = callback.bind(thisArg);
+ }
for (var i = 0; i < this._items.length; i++)
callback(this._items[i], i, this);
@@ -294,12 +299,17 @@ var StringSetPrototype = {
// Call a callback function on each element of the set. If the set
// is changed by the callback, the results are undefined.
- each: function(callback) {
+ // Callback takes the same parameters as the forEach method of
+ // arrays: value, index, set
+ // Takes an optional parameter that sets what this is bound to.
+ each: function(callback, thisArg) {
// If there's no callback, don't bother.
if (!callback) return;
+
+ if (thisArg) callback = callback.bind(thisArg);
for (var x in this._items)
- callback(this._items[x]);
+ callback(this._items[x], x, this);
}
}

0 comments on commit bf9212f

Please sign in to comment.