Simulation code of jquery implementation principle-2 Data Section

Source: Internet
Author: User
Tags delete cache

Of course, this data must be accessed through attributes. But what if there are multiple attributes ?, Do you want to define multiple attributes ?, What is the name of an attribute? Will it conflict with other attributes?
In jquery, the private data extended for DOM objects can be represented by one object. Multiple data are represented by multiple attributes of this object. In order to be able to find this extended data object through the DOM object without conflicting with other existing attributes, the expando constant in jquery indicates the property name of the extended object, the expando value is calculated. The value of this attribute is used to find the key value of the extended object.
For example, we can define the expando value as "jquery1234". Then, we can add the property "jquery1234" to each DOM object. The value of this property can be a key, for example, 1000.
The cache on the jquery object is used to save all the extended objects of the object. This object can be seen as a dictionary. The attribute name is the key value, and the corresponding value is the extended data object.
That is to say, there will be a 1000 member in the cache of the jquery object. The referenced object of this Member is the private extension object of the DOM object No. 1000. The private data of the 1000 member will exist on this object.
When a DOM object needs to obtain extended data, it first obtains a key value through the expando attribute of the object, and then uses the key value to jquery. get your own extension object in the cache, and then read and write data on the extension object. CopyCode The Code is as follows: // <Reference Path = "jQuery-core.js"/>
// Common method
Function now (){
Return (new date). gettime ();
}
// Expand the attribute name of the data and dynamically generate it to avoid conflicts with existing attributes
VaR expando = "jquery" + now (), UUID = 0, windowdata = {};
Jquery. cache = {};
Jquery. expando = expando;
// Data management, which can store private data for DOM objects and read stored 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 and delete the data stored on the object
Jquery. FN. removedata = function (key ){
Return this. Each (function (){
Jquery. removedata (this, key );
})
}

// Save data for the element
Jquery. Data = function (ELEM, name, data) {// #1001
// Obtain the key value of the element to save the data
VaR id = ELEM [expando], cache = jquery. cache, thiscache;
// If no ID is available, the value cannot be set.
If (! ID & typeof name = "string" & Data = undefined ){
Return NULL;
}
// Compute a unique ID for the element
// Calculate a unique key value for the element
If (! ID ){
Id = ++ UUID;
}
// If it has not been saved
If (! Cache [ID]) {
ELEM [expando] = ID; // Save the key value on the element
Cache [ID] ={}; // create an object on the cache to save the value corresponding to the element
}
// Obtain the data object of this element
Thiscache = cache [ID];
// Prevent overriding the named cache with undefined values
// Save the value
If (Data! = Undefined ){
Thiscache [name] = data;
}
// Return the corresponding value
Return typeof name = "string "? Thiscache [name]: thiscache;
}
// Delete the stored 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 whether the object is empty
Jquery. isemptyobject = function (OBJ ){
// Traverses the attributes of an element. If the attribute is required, false is returned. Otherwise, true is returned.
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 The Code is as follows: // data operation
$ ("# 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.