Home >Web Front-end >JS Tutorial >jQuery source code analysis notes (6) jQuery.data_jquery
The code in the data part starts at line 1381. The first few key lines of code:
// 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;
}
}
}