jquery implementation of the principle of the simulation code-2 data parts _jquery

Source: Internet
Author: User
Tags unique id uuid delete cache
This data is, of course, accessed through attributes, but what about multiple attributes? , do you want to define multiple properties? , what is the name of the property? Will it conflict with other attributes?
In JQuery, private data extended for DOM objects can be represented by an object, and multiple data is represented by multiple properties of the object. To be able to find this extended data object through a DOM object without conflict with other existing attributes, the expando value is computed in jQuery by expando this constant to represent the property name of the extended object. The value of this property is the key value used to find the extended object.
For example, we can define the value of expando as "jQuery1234", so we can add this property named "jQuery1234" for each DOM object, and the value of this property can be a key, for example, 1000.
The cache on the JQuery object is used to hold objects that are extended by the object, which can be considered as a dictionary, the attribute name is the key value, and the corresponding value is the extended data object.
That is, on the cache of the JQuery object, there will be a 1000 member that references the object that is the private extension object of the DOM 1000th object. The private data for member 1000th will be present on this object.
When a DOM object needs to obtain the extended data, it first obtains a key value through the object's Expando property, then obtains its own extension object through the key value to the Jquery.cache, then reads and writes the data on the extended object.
Copy Code code as follows:

<reference path= "Jquery-core.js"/>
Common methods
function Now () {
Return (new Date). GetTime ();
}
Expands the attribute name of the data, dynamically generates, avoids conflicts with the existing attribute
var expando = "JQuery" + Now (), uuid = 0, windowdata = {};
Jquery.cache = {};
Jquery.expando = expando;
Data management, you can save private data on a DOM object, and you can read the saved data
JQuery.fn.data = function (key, value) {
Read
if (value = = undefined) {
Return Jquery.data (This[0], key);
}
else {//settings
This.each (
function () {
Jquery.data (this, key, value);
}
);
}
}
Remove data, delete data saved on object
JQuery.fn.removeData = function (key) {
Return This.each (function () {
Jquery.removedata (this, key);
})
}

Saving data for an element
Jquery.data = function (elem, name, data) {//#1001
Gets the key value of the element's saved data
var id = Elem[expando], cache = Jquery.cache, thiscache;
Cannot take a value without an ID
if (!id && typeof name = = "string" && data = = undefined) {
return null;
}
Compute a unique ID for the element
Computes a unique key value for an element
if (!id) {
id = ++uuid;
}
If it has not been saved
if (!cache[id]) {
Elem[expando] = ID; To save key values on an element
Cache[id] = {}; Create an object on the cache to hold the corresponding value of the element
}
Gets the data object for this element
Thiscache = Cache[id];
Prevent overriding the named cache with undefined values
Save value
if (data!== undefined) {
Thiscache[name] = data;
}
Returns the corresponding value
return typeof name = = = "string"? Thiscache[name]: Thiscache;
}
To delete saved data
Jquery.removedata = function (elem, name) {//#1042
var id = Elem[expando], cache = Jquery.cache, Thiscache = Cache[id];
If we want to remove a specific section of the element ' s data
if (name) {
if (Thiscache) {
Remove the section of cache data
Delete Thiscache[name];
If we ' ve removed all the data, remove the element ' s cache
if (Jquery.isemptyobject (Thiscache)) {
Jquery.removedata (Elem);
}
}
Otherwise, we want to remove all of the element ' s data
} else {
Delete Elem[jquery.expando];
Completely remove the data cache
Delete Cache[id];
}
}
Check if the object is empty
Jquery.isemptyobject = function (obj) {
To traverse the attributes of an element, return false only if the property is returned, or return True
for (var name in obj) {
return false;
}
return true;
}
Check whether it is a function
jquery.isfunction = function (obj) {
var s = tostring.call (obj);
return Tostring.call (obj) = = "[Object Function]";
}


The following script can save or read the extended data of an object.

Copy Code code as follows:

Data manipulation
$ ("#msg"). Data ("name", "Hello, World.");
Alert ($ ("#msg"). Data ("name");
$ ("#msg"). Removedata ("name");
Alert ($ ("#msg"). Data ("name");

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.