Ticket #2445 (closed bug: wontfix)
"this" refers to window object in $.map()
| Reported by: | ratbeard | Owned by: | flesler |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.3 |
| Component: | core | Version: | 1.2.3 |
| Keywords: | map | Cc: | |
| Blocking: | Blocked by: |
Description
Hello,
I found that 'this' inside of the callback function in $.map() refers to the window object, and not the current unmapped value. This is inconsistent with the other iteration functions, i.e. $().map, $.each, and with the jQuery documentation at http://docs.jquery.com/Utilities/jQuery.map#arraycallback Here's an example that shows the different behavior between using 'this' and the passed in element object:
$.map(["hey"], function(el) { return $('<div>').text(el + ' yo'); })[0].appendTo('body');
--> "hey yo"
$.map(["hey"], function(el) { return $('<div>').text(this + ' yo'); })[0].appendTo('body');
--> "[object Window] yo"
Its a small fix - just calling the callback in the elements scope. I've attached a diff that fixes the problem.
Mike
Attachments
Change History
Changed 5 years ago by ratbeard
-
attachment
jquery-1.2.3-fix.txt
added
Changed 5 years ago by ratbeard
-
attachment
jquery-map-test.html
added
A test page that shows the problem
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

diff generated from my patch and jquery-1.2.3.js