jQuery data cache usage analysis

This paper analyzes the jQuery data cache usage. Share to you for your reference. details as follows:

In the jQuery API Help documentation, jQuery described the role of data cache: for accessing data over an element while avoiding the risk of circular references.

A, defines the data cache

Use $ (selector) .data (name, value) jQuery method may cache data object definition. The cache data is stored in the matched set of DOM elements in all of the DOM element.

var $link = $('a');
$link.data('linkType', 'home');

Description: $ (selector) .data (name, value) can be stored in any format of data elements matching the DOM, rather than the string.

Second, get the data cache

In this case, only one parameter can be, this parameter specifies the name of the cached data.

var linkType = $link.data('linkType'); //'home'

Note: If the read cache data does not exist, it returns undefined value; jQuery set point if the plurality of elements, then only the cache data corresponding to the first return element.

Third, remove cached data

removeData () function can be deleted cache data specified name, and returns the corresponding jQuery object.

// delete the cached data at the same time, it returns the corresponding jQuery object.

var $a = $link.removeData('linkType');  

Four, jQuery data cache Use

With the call data () function increase the number, or improper use, will make the object cache rapid expansion, and ultimately affect the performance of the program.
Therefore, when using jQuery data caching function, should be cleared cache object. Providing removeData jQuery () function to manually clear the cache data. The operating mechanism jQuery framework, the following situations need to manually clear the data cache.

  • Execute remove for elem () operation, jQuery will automatically clear the cache objects that may exist.
  • Elem performed on empty () operation, if the current child element elem data cache exists, jQuery will clear the data buffer sub-objects may be present.
  • jQuery clone replicated node () method does not copy data cache. clone (true) will be.
  • $ (Selector) .clone (includeEvents)
    includeEvents
    optional. Boolean value. All events Specifies whether to copy the elements of the process.
    By default, the copy does not contain event handlers.
    clone (true) method is to copy an element and all events,
    clone () method is to copy an element does not contain all of its events.

Guess you like

Origin blog.csdn.net/qwe435541908/article/details/80605262