Add context argument to Number#times

This commit is contained in:
Samuel Lebeau 2008-05-05 15:17:08 +02:00 committed by Tobie Langel
parent 49b921a5c7
commit 15b43b7633
2 changed files with 12 additions and 2 deletions

View File

@ -7,8 +7,8 @@ Object.extend(Number.prototype, {
return this + 1;
},
times: function(iterator) {
$R(0, this, true).each(iterator);
times: function(iterator, context) {
$R(0, this, true).each(iterator, context);
return this;
},

View File

@ -30,5 +30,15 @@ new Test.Unit.Runner({
this.assertEqual('null', Number.NaN.toJSON());
this.assertEqual('0', (0).toJSON());
this.assertEqual('-293', (-293).toJSON());
},
testNumberTimes: function() {
var results = [];
(5).times(function(i) { results.push(i) });
this.assertEnumEqual($R(0, 4), results);
results = [];
(5).times(function(i) { results.push(i * this.i) }, { i: 2 });
this.assertEnumEqual([0, 2, 4, 6, 8], results);
}
});