JQuery Source Analysis Notes (6) Jquery.data_jquery

Source: Internet
Author: User
Tags unique id uuid delete cache
The code in the data section starts at 1381 lines. The first few lines of critical code:
Copy Code code as follows:

Jquery.extend ({
Where data is stored, the key to achieve the core
Cache: {},
Seed for allocation ID
uuid:0,
To distinguish the data stored by different jquery instances, use the prefix +jquery version number + random number as key
expando: "JQuery" + (JQuery.fn.jquery + math.random ()). Replace (/\d/g, ""),
The following elements have no data:embed and applets (this thing is still alive), except for object outside of Flash.
NoData: {
"Embed": true,
"Object": "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
"Applet": True
}
});

External interfaces Call 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 it is added, and Removedata deletes data by using delete or setting to NULL.
The data section of the code clearly distinguishes between the JS object and the DOM object save, this is to solve some of the browser memory leak problem. In low version ie, when a circular reference occurs between the DOM and the JS object, the GC cannot handle it correctly. See Understanding and solving Internet Explorer leak Patterns. As for COM objects, because the object element has been restricted to no data, the problem is bypassed.
Copy Code code as follows:

Data:function (elem, name, data, Pvt) {
If you belong 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 the Cache,js object directly to the Elem
Cache = Isnode? JQuery.cache:elem,
If the Elem Jquery.expando already has a value, reuse
id = isnode? Elem[jquery.expando]: Elem[jquery.expando] && Jquery.expando;
<pre class=brush:;gutter:true;><code>//data is not defined, indicating that the current call is querying the database, 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) {
Incrementing a unique ID with a UUID seed, only required by the DOM element. Because there is a need for global cache
Elem[jquery.expando] = id = ++jquery.uuid;
} else {
id = Jquery.expando;
}
}
Empty the original value
if (!cache[id]) {
Cache[id] = {};
if (!isnode) {
Cache[id].tojson = Jquery.noop;
}
}
Use extend to extend cache, 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 previous section is similar to data//...//Some browsers do not support the delete operation on the element and check this browser feature in the Jquery.support. If delete fails, it is set 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 you have data, clear it once and then set it up to increase performance
if (Internalcache) {
cache[id] = {};
cache[ID [Internalkey] = Internalcache;
There's no more data, just delete it all
else if (Isnode) {
If delete is supported, delete.
IE uses removeattribute, so try it once. Failure 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;
}
}
}

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.