javascript jQuery.data 和 jQuery._data(下划线数据)有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7788353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between jQuery.data and jQuery._data ( underscore data )?
提问by THEtheChad
While going through the source, I noticed that 'toggle' supposedly uses jQuery._data
to store the state of the element. I examined the jQuery.cache
object in chrome and found that the element's data object had yet another object under it prepended by the word jQuery with a number that I'm guessing uniquely identifies it. However, I saw no data regarding the state of the element. Simply {olddisplay: 'block'}
. Any clues as to the purpose of jQuery._data and how it works per se?
在查看源代码时,我注意到 'toggle' 据称用于jQuery._data
存储元素的状态。我检查了jQuery.cache
chrome 中的对象,发现该元素的数据对象下还有另一个对象,前面有 jQuery 一词,其中有一个我猜是唯一标识它的数字。但是,我没有看到有关元素状态的数据。简直了{olddisplay: 'block'}
。关于 jQuery._data 的目的及其本身如何工作的任何线索?
I've been staring at the source all day .... please don't tell me to view the source. My eyes and brain will thank you.
整天盯着源码看....请不要告诉我查看源码。我的眼睛和大脑会感谢你。
回答by jfriend00
jQuery uses _data in order to set the 'pvt' flag for data it stores on the object. The pvt
is used so that when you request public data from the object, pvt data is not returned. This is to keep jQuery's internal use of the .data()
mechanism (like what toggle does) from effecting the public use of .data()
.
jQuery 使用 _data 为它存储在对象上的数据设置“pvt”标志。在pvt
被使用,因此,当你从对象请求公开数据,不返回PVT数据。这是为了防止 jQuery 对.data()
机制的内部使用(就像切换所做的那样)影响.data()
.
You can see this declaration in the jQuery source:
您可以在 jQuery 源代码中看到此声明:
// For internal use only.
_data: function( elem, name, data ) {
return jQuery.data( elem, name, data, true );
},
Which just calls jQuery.data
and forces the fourth parameter (which is privacy) to be true. When retrieving data, if the pvt
flag is set, then it is retrieved in a slightly different way. The public interfaces to .data()
do not expose the pvt
flag.
这只是调用jQuery.data
并强制第四个参数(即隐私)为真。检索数据时,如果pvt
设置了标志,则检索方式略有不同。公共接口.data()
不公开pvt
标志。
You can see an example of pvt
handling here in this part of jQuery.data()
:
您可以pvt
在以下部分中看到处理示例jQuery.data()
:
// An object can be passed to jQuery.data instead of a key/value pair; this gets
// shallow copied over onto the existing cache
if ( typeof name === "object" || typeof name === "function" ) {
if ( pvt ) {
cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
} else {
cache[ id ] = jQuery.extend(cache[ id ], name);
}
}
and then later in that same function, this comment is pretty descriptive:
然后在同一个函数中,这个注释非常具有描述性:
// Internal jQuery data is stored in a separate object inside the object's data
// cache in order to avoid key collisions between internal data and user-defined
// data
if ( pvt ) {
if ( !thisCache[ internalKey ] ) {
thisCache[ internalKey ] = {};
}
thisCache = thisCache[ internalKey ];
}