admin管理员组文章数量:1336399
When I store an object like {a: 1, b: 2 }
in jQuery's data, does it copy the object or save a reference to it?
I have a huge object and I want different elements to store different references from different points to the same object, and I don't want it to get copied.
Like
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.c.d.jQuery);
When I store an object like {a: 1, b: 2 }
in jQuery's data, does it copy the object or save a reference to it?
I have a huge object and I want different elements to store different references from different points to the same object, and I don't want it to get copied.
Like
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.c.d.jQuery);
Share
Improve this question
edited Nov 9, 2015 at 2:06
pnuts
59.5k11 gold badges91 silver badges141 bronze badges
asked Apr 17, 2011 at 16:32
qwertymkqwertymk
35.4k30 gold badges124 silver badges184 bronze badges
3 Answers
Reset to default 5According to my jsfiddle test, it stores a reference.
If I do this:
$('div').data('info', obj.b);
obj.b.apple = 'bleuch';
alert($('div').data('info').apple);
It alerts "bleuch", showing that a reference to the original object is being stored.
It will save a reference to it.
Javascript objects are never copied, unless you explicitly make a copy.
From http://api.jquery./data/
"The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery)."
本文标签: javascriptjquery data() methodStack Overflow
版权声明:本文标题:javascript - jquery .data() method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742373629a2462721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论