JQuery source code analysis notes (6) jQuery. data

Source: Internet
Author: User
Tags delete cache

The data code starts from line 1. The first few lines of key code:
Copy codeThe Code is as follows:
JQuery. extend ({
// Key implementation core for data storage
Cache :{},
// Seed used for ID allocation
Uuid: 0,
// To differentiate the data stored in different jQuery instances, use the prefix + jQuery version number + random number as the Key
Expando: "jQuery" + (jQuery. fn. jquery + Math. random (). replace (/\ D/g ,""),
// The following elements do not have Data: embed and applet (is this thing still alive), except for objects other than Flash.
NoData :{
"Embed": true,
"Object": "clsid: D27CDB6E-AE6D-11cf-96B8-444553540000 ",
"Applet": true
}
});

The external interfaces call two internal functions: jQuery. data (elem, name, data, pvt) and jQuery. removeData (elem, name, pvt ). The removeData logic is similar to data, except that data is added to data, while removeData uses delete or is set to null to delete data.
The code in the data section clearly distinguishes the storage of JS objects and DOM objects to solve the memory leakage problem of Some browsers. In earlier versions of IE, GC cannot be correctly processed when circular references occur between DOM and JS objects. See Understanding and Solving Internet Explorer Leak Patterns. As for the COM object, this problem is bypassed because the object element does not have data.
Copy codeThe Code is as follows:
Data: function (elem, name, data, pvt ){
// If it belongs to the element defined in noData
If (! JQuery. acceptData (elem )){
Return;
}
Var internalKey = jQuery. expando,
GetByName = typeof name = "string ",
ThisCache,
IsNode = elem. nodeType,
// DOM elements must be stored in the Cache, and JS objects must be directly stored in elem.
Cache = isNode? JQuery. cache: elem,
// If elem's jQuery. expando already has a value, reuse it.
Id = isNode? Elem [jQuery. expando]: elem [jQuery. expando] & jQuery. expando;
<PRE class = brush:; gutter: true;> <CODE> // data is undefined, indicating that the current call is to query data, but the object does not have any data, directly return
If ((! Id | (pvt & id &&! Cache [id] [internalKey]) & getByName & data === undefined ){
Return;
}
If (! Id ){
If (isNode ){
// Use the uuid seed to incrementally allocate a unique ID, which is only required by DOM elements. Because it needs to exist in the global cache
Elem [jQuery. expando] = id = ++ jQuery. uuid;
} Else {
Id = jQuery. expando;
}
}
// Clear the original value
If (! Cache [id]) {
Cache [id] = {};
If (! IsNode ){
Cache [id]. toJSON = jQuery. noop;
}
}
// Extended cache with extend to add an attribute to save data
If (typeof name = "object" | typeof name = "function "){
If (pvt ){
Cache [id] [internalKey] = jQuery. expand (cache [id] [internalKey], name );
} Else {
Cache [id] = jQuery. extend (cache [id], name );
}
}
ThisCache = cahce [id];
// Avoid Key conflict
If (pvt ){
If (! ThisCache [internalKey]) {
ThisCahce [internalKey] = {};
}
ThisCache = thisCache [internalKey];
}
If (data! = Undefined ){
ThisCache [jQuery. camelCase (name)] = data;
}
Return getByName? ThisCache [jQuery. camelCase (name)]: thisCache;
}
RemoveData: function (elem, name, pvt) {// the previous part is similar to data //... // Some browsers do not support the delete operation on the Element. this browser feature has been checked in support. // If delete fails, set it to null first. If (jQuery. support. deleteExpando | cache! = Window) {delete cache [id];} else {cache [id] = null ;}
<PRE class = brush:; gutter: true;> <CODE> var internalCache = cache [id] [internalKey];
// If there is still data, clear it and set it again to increase performance
If (internalCache ){
Cache [id] = {};
Cache [id] [internalKey] = internalCache;
// If no data exists, delete it all.
} Else if (isNode ){
// If delete is supported, delete it.
// IE uses removeAttribute, so try it once. If it fails, it can only be set to null.
If (jQuery. support. deleteExpando ){
Delete elem [jQuery. expando];
} Else if (elem. removeAttribute ){
Elem. removeAttribute (jQuery. expando );
} Else {
Elem [jQuery. expando] = null;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.