JQuery. data () method and Memory leakage

Source: Internet
Author: User

In the official document of jQuery, users are prompted that this is a low-level method and should be replaced by the. data () method. $. Data (element, key, value) can append any type of data to the DOM element, but the memory leakage caused by loop reference should be avoided. The original Article is as follows:

The jQuery. data () method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks. we can set several distinct values for a single element and retrieve them later:

However, this method is not only problematic. In jquery forum, this issue was discussed in depth. robert katic proposed a solution. The $. data () method is applied to the Host object and the running is optimized. However, if you use this method on the local image, the results may not be satisfactory. Under normal circumstances, an element can be deleted using the. remove () method and its data can be cleared. However, local objects cannot be completely deleted. The related data continues until the window object is closed. Similarly, these problems also exist in the event object, because the event processor (handlers) also uses this method for storage.

The simplest way to solve this problem is to store the data to a new attribute of the local object. That is:

//...
If (elem. nodeType ){
Cache [id] = dataObject;
Elem [expando] = id;
} Else {
Elem [expando] = dataObject;
}
//...

However, this method is powerless once inheritance is involved. Labels:

Var parent = {};
Var childA = Object. create (parent );
Var childB = Object. create (parent );

$. Data (parent, "foo", "parent value ");

// This may even be intentional
$. Data (childA, "foo") // => "parent value"
$. Data (childB, "foo") // => "parent value"

// This may NOT be intentional
$. Data (childA, "foo", "childA value ");
$. Data (parent, "foo") // => "childA value"
$. Data (childB, "foo") // => "childA value"

At the beginning, the object storing data does not exist. Therefore, an object is created to store new values,

Now, we try to modify the same data as the childA object.

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.