Event callback: function(eventObject) { this = domnode }// Because 
that's the way events have always worked
each() callback: function(index, sameAsThis) { this = item/domnode } // 
Keeping the pattern
map() callback: function(index, sameAsThis) { this = item/domnode } // 
Keeping the pattern
filter() callback: function(index) { this = item/domnode } // sorta 
keeping the pattern
vs.
Event callback: function(eventObject) { this = domnode } // event 
callbacks MUST use this as domnode, otherwise you destroy the api it was 
based on
each() callback: function(item, index?) { ... }
map() callback: function(item, index?) { ... }
filter() callback: function(item, index?) { ... }

Whether you like it or not, jQuery is a library for working with the 
dom. Thus when created each/map/filter had nothing to do with the array 
methods that have only been put into browsers recently (yes, recently... 
you are comparing standards set by new features, with decisions that 
were made years ago; not the best comparison), they were created to give 
control over the jQuery object's list of dom nodes. Thus, since all the 
other apis which worked with dom nodes used `this` to refer to the dom 
node that same api was used when writing those methods.
It's only a side effect that these methods happen to be usable on 
non-domlist arrays. Though frankly, imho that is abuse, these methods 
are not meant for that purpose, you can plainly see that in how $.fn.map 
differs from Array.prototype.map in how it folds arrays into the list 
rather than adding them as items.

~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]

Pauan wrote:
> And how is it inferior to set the current item as an argument to the
> callback? This is how Array.prototype.forEach works. It causes less
> problems while providing the exact same functionality. Also, event
> handlers are not the same as functions used to iterate over an object,
> nor do they serve the same purpose, so one model doesn't necessarily
> apply to the other.
>
> On Aug 11, 6:10 am, Scott González <scott.gonza...@gmail.com> wrote:
>   
>> There definitely is a reason for it.  jQuery is a library for working
>> with the DOM, it is not a library meant to fix every problem with
>> JavaScript or to try to extend JavaScript in every possible way.  When
>> using jQuery specifically to work with the DOM (probably >90% of what
>> you're using it for, and 100% of what the average user uses it for),
>> it makes perfect sense to set the context to the current item.  Also,
>> native event handlers set the context to the event target, so jQuery
>> had an existing model to follow.
>>     

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to