Skip to main content

Bug Tracker

Side navigation

#2445 closed bug (wontfix)

Opened March 03, 2008 03:32AM UTC

Closed July 09, 2008 07:01PM UTC

"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:
Blocked by: Blocking:
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 (2)
  • jquery-1.2.3-fix.txt (0.1 KB) - added by ratbeard March 03, 2008 03:32AM UTC.

    diff generated from my patch and jquery-1.2.3.js

  • jquery-map-test.html (0.6 KB) - added by ratbeard March 03, 2008 03:35AM UTC.

    A test page that shows the problem

Change History (2)

Changed May 16, 2008 03:33AM UTC by flesler comment:1

owner: → flesler
status: newassigned

Yes, this is pretty clear.

The docs say:

this; // unmapped

I'm not sure if that means "the unmapped element" or "unmapped" as in, not-forced => window.

I'll ask around and will fix this if correct.

Changed July 09, 2008 07:01PM UTC by flesler comment:2

milestone: 1.2.41.3
resolution: → wontfix
status: assignedclosed

It was fixed in the docs. The this won't point to the element when mapping (or 'grepping').