Ticket #10266 (closed bug: wontfix)
Undefined values passed to $(element).data(key,value) break chaining
| Reported by: | jsoverson@… | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | data | Version: | 1.6.4rc1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by rwaldron) (diff)
Example here:
The problem occurs when jquery tests for definedness of the passed value. If it is undefined the logic assumes you want the value of the key, which is likely not the assumption a user makes when they pass in two arguments. The problem experienced here is largest during chaining where the returned value breaks the rest of the chain.
This issue is also what likely triggered ticket but that ticket was closed prematurely due to the inability to reproduce.
Change History
comment:2 Changed 20 months ago by anonymous
I think setting a value to undefined is a valid use case, but that might be a different discussion.
I think, regardless, by sending an argument in place of the "value" field, retrieving the previously set value is the least expected result.
I threw in the fix and test case here since I was already working in the code: https://github.com/jquery/jquery/pull/501
comment:3 Changed 20 months ago by timmywil
- Priority changed from undecided to low
- Component changed from unfiled to data
All jQuery getters/setters check for undefined. Retrieving an item specifically set to undefined and trying to retrieve one that doesn't exist will produce exactly the same result, so there shouldn't be a reason to set data to undefined. I say use removeData.
comment:4 Changed 20 months ago by rwaldron
- Description modified (diff)
-1 This request has been made before for other methods and we've argued the color of the explicit undefined parameter bikeshed plenty of times.
comment:5 Changed 20 months ago by rwaldron
- Status changed from new to closed
- Resolution set to wontfix
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

jQuery.fn.data appears to check for the presence of the value argument by the expression value !== undefined. Another way would be to check the arguments.length, but if that was done then $(el).data('a', undefined) would appear to set a to undefined, but it doesn't (jQuery.data also uses the arg === undefined test). So, if a was already set to 123, it'll remain that way. IMO, this'd be misleading. The question is: should passing an argument, even if it's undefined, still be construed as intentionally passing that argument, or alternatively: willfully accessing the getter functionality of jQuery.fn.data??
What should jQuery(el).data('foo', undefined) be expected to do?
Anyway, like I said, a fix would be easy enough -- checking for the appropriate argument-number in arguments.