If you store an object (or array) in .data()
, then you actually store a reference to it, so if you do:
var obj = { key: 'value' } $(el).data('obj') = obj; obj.key = 'new value';
$(el).data('obj').key
will also be new value
, because it is the same object.
However, if the stored value is a simple type (for example, a number or a string) in which its copy will be stored:
var n = 5; $(el).data('obj') = n; n++;
$(el).data('obj')
will still be 5.
Alnitak Apr 22 2018-11-21T00: 00Z
source share