Home > Web Front-end > JS Tutorial > body text

jQuery source code analysis notes (6) jQuery.data_jquery

WBOY
Release: 2016-05-16 18:06:00
Original
947 people have browsed it

The code in the data part starts at line 1381. The first few key lines of code:

Copy code The code is as follows:

jQuery.extend( {
// The place where data is stored, the key implementation core
cache: { },
// The seed used to assign IDs
uuid: 0,
// In order to distinguish different jQuery instances Stored data uses a random number prefixed with jQuery version number as Key
expando: "jQuery" (jQuery.fn.jquery Math.random()).replace(/D/g, ""),
// The following elements have no Data: embed and applet (are they still alive?), objects other than Flash.
noData: {
"embed": true,
"object": "clsid:D27CDB6E-. AE6D-11cf-96B8-444553540000",
"applet": true
}
});

The external interface calls two internal functions: jQuery.data (elem, name, data, pvt) and jQuery.removeData(elem, name, pvt). The logic of removeData is similar to data, except that data is added, 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. This is to solve the memory leak problem of some browsers. In earlier versions of IE, when a circular reference occurs between the DOM and JS objects, the GC cannot handle it correctly. See Understanding and Solving Internet Explorer Leak Patterns. As for COM objects, this problem is bypassed because the object element has been restricted to have no data.
Copy code The code is as follows:

data: function(elem, name, data, pvt) {
// If it belongs to an element defined in noData
if(!jQuery.acceptData(elem)) {
return;
}
var internalKey = jQuery.expando,
getByName = typeof name === "string",
thisCache,
isNode = elem.nodeType,
// DOM elements need to be saved in Cache, and JS objects are saved directly to elem
cache = isNode ? jQuery .cache : elem,
// If elem’s jQuery.expando already has a value, reuse
id = isNode ? elem[jQuery.expando] : elem[jQuery.expando] && jQuery.expando;
// data is undefined, indicating that the current call is to query data, but the object does not have any data, and returns directly 
if((!id || (pvt && id && !cache[id][internalKey])) && getByName && data === undefined) {
return;
}
if(!id) {
if(isNode) {
// Incrementally assign a unique ID using uuid seed, only required for DOM elements. Because it needs to be stored 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;
}
}
// Use extend to extend cache and 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 conflicts
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 front part is similar to data // ... // Some browsers do not support the delete operation on Element. Check this browser feature in jQuery.support. // If delete fails, set it to null first. if ( jQuery.support.deleteExpando || cache != window ) { delete cache[ id ]; } else { cache[ id ] = null; }
< ;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 there is no more data, delete it all
} else if ( isNode ) {
// If delete is supported, delete it.
// IE uses removeAttribute, so try it once. If it fails again, 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template