_each method and context

28 views
Skip to first unread message

Yaffle

unread,
Feb 16, 2009, 7:29:55 AM2/16/09
to Prototype: Core
Hello,

Why with JavaScript <= 1.5, Array.prototype._each method doesn't use
a context object

like

Array.prototype.forEach(callback[, thisObject]) in JavaScript 1.6

or

Enumerable.prototype.each(iterator[, context]) ?

thank you.

kangax

unread,
Feb 16, 2009, 11:16:05 AM2/16/09
to Prototype: Core
Because `Array.prototype._each` (defined by Prototype.js) is bound to
custom `each` implementation when native `Array.prototype.forEach` is
unavailable (e.g. in older versions of Firefox). That custom
implementation currently looks like:

function each(iterator) {
for (var i = 0, length = this.length; i < length; i++)
iterator(this[i]);
}

while, to be consistent with `forEach`, it would need to look like:

function each(iterator, context) {
for (var i = 0, length = this.length; i < length; i++)
iterator.call(context, this[i], i, this);
}

--
kangax
Message has been deleted

Tobie Langel

unread,
Feb 16, 2009, 6:09:23 PM2/16/09
to Prototype: Core
Can you open a ticket for this?

Thanks,

Tobie

kangax

unread,
Feb 16, 2009, 11:13:15 PM2/16/09
to Prototype: Core
On Feb 16, 6:09 pm, Tobie Langel <tobie.lan...@gmail.com> wrote:
> Can you open  a ticket for this?

Sure, patch and test attached.
http://prototype.lighthouseapp.com/projects/8886-prototype/tickets/554-_each-to-match-native-foreach-behavior

--
kangax
Reply all
Reply to author
Forward
0 new messages