jQuery.data和jQuery._data(下划线数据)有什么区别? [英] What's the difference between jQuery.data and jQuery._data ( underscore data )?

查看:558
本文介绍了jQuery.data和jQuery._data(下划线数据)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览源代码时,我注意到'toggle'应该使用 jQuery._data 来存储元素的状态。我检查了chrome中的 jQuery.cache 对象,发现该元素的数据对象下面还有另一个对象,它由jQuery一词前面加上一个我猜是唯一标识的数字它。但是,我没有看到关于元素状态的数据。只需 {olddisplay:'block'} 。有关jQuery._data目的的任何线索及其本身如何运作?

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?

我一直在盯着源头......请不要告诉我查看来源。我的眼睛和大脑会感谢你。

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.

推荐答案

jQuery使用_data为它存储的数据设置'pvt'标志物体。使用 pvt ,以便在从对象请求公共数据时,不会返回pvt数据。这是为了保持jQuery内部使用 .data()机制(就像切换所做的那样)来影响公共使用 .data()

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源代码中看到此声明:

You can see this declaration in the jQuery source:

// For internal use only.
_data: function( elem, name, data ) {
    return jQuery.data( elem, name, data, true );
},

其中只调用 jQuery.data 并强制第四个参数(即隐私)为真。检索数据时,如果设置了 pvt 标志,则会以稍微不同的方式检索它。 .data()的公共接口不公开 pvt 标志。

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 处理的示例:

You can see an example of pvt handling here in this part of 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 ];
}

这篇关于jQuery.data和jQuery._data(下划线数据)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆