Proposal to change the design of .data() slightly
|Reported by:||latchkey@…||Owned by:|
Dear jQuery experts and my mentors:
I know that this has been brought up multiple times in other ways, but I'm going to bring it up again, in hopefully a different way, so please be kind enough to hear me out.
Suppose you have this:
.data('foo') === 'bar'
Ok, no big deal. String in, string out.
.data('ack') === 1.4
Yes, I'm well aware that putting a float will return me a float, who's value will lose the precision of the two 0's. That is fine, no big deal.
The issue I'm having with .data is when the values of data are assigned from an element in the DOM like this:
data('id') === 1.4 (the float, instead of the string 1.400)
What I would like to propose is that:
#1. .data() be changed to always return the same type of what goes into data() #2. make what goes into data(), from elements, always be a string since you can't really assume what the type is or assume that you are doing a conversion here.
This will do four major things:
#1. not break existing code. we can't have that. #2. negate the need to use attr() to retrieve the unmolested value of data() from elements. #3. prevent me from having to write everything as data-id="'1.400'" (using single quotes). this is arguably very error prone. #4. remove a potentially bug inducing bit of design. if i suddenly decide to change my id from an integer to a float (this happened recently, so that is what brought this up, so i know it is a real world issue).
Again, I thank you for your time and patience and understanding.
Change History (10)
comment:1 Changed 5 years ago by dmethvin
- Resolution set to duplicate
- Status changed from new to closed