Jquery.data () method

When I store an object as {a: 1, b: 2 } in jQuery data, does it copy the object or keep a reference to it?

I have a huge object, and I want different elements to keep different links from different points to the same object, and I do not want it to be copied.

how

 var obj = { a: { one: 1, two: 2 }, b: { apple: 'yummy', banana: 'ehh' } c: { d: { 'jQuery': jQuery } e: ['You get the point'] } }; $('div').data('info', obj.b); $('#JQ').data('jq_reference', obj.cdjQuery); 
+8
javascript jquery object
source share
3 answers

According to my jsfiddle test , it saves the link.

If I do this:

 $('div').data('info', obj.b); obj.b.apple = 'bleuch'; alert($('div').data('info').apple); 

He warns "bleuch" by indicating that a reference to the source object is stored.

+5
source share

It will save a link to it.

Javascript objects are never copied unless you explicitly make a copy.

+1
source share

From http://api.jquery.com/data/

"The data attributes are pulled out the first time you access the data properties, and then are no longer available or changed (all data values ​​are then stored inside jQuery).

0
source share

All Articles