jquery Object data Cache principle and Jquery.data explanation

Source: Internet
Author: User

Online there's a lot to teach you how to use Jquery.data (..) To implement the data cache, but there are two users who often use data ([Key],[value]) and Jquery.data (Element,[key],[value]) There are few articles that tell the difference between them, so I used the research to share with you.
$ ("") The difference between data ([Key],[value]) and Jquery.data (Element,[key],[value])
Both of these functions are used to store data on the element as well as the usual data cache, all return jquery object, when I used it in both of them really scare me, the difference can be big, really do not know, a scare jump. Look at the example first, and then according to the source code analysis. JS code:  <div id= "test2"  onclick= "Test ()" >test2</div>              <div id= "ABC3"  onclick= "Test ()" >test3</div>              <div id= "Test"  onclick= " Test () ">test</div>             <p  Id= "TTT" >aaaa</p>       <script>          $ (document). Ready (function () {           $ (" #test "). Click (function () {           alert (" JQUERY ");                 var e=$ ("div");// Two jquery objects     &nbs definedp;   var w=$ ("div");//e is not equal to W.            //First uses the data ([Key],[value]) usage.          $ (E). Data ("A", "AAAA"),/////Save Key on E and W,          $ (W). Data ("A", "wwww");//  see if it will overwrite the previous one, although it is stored on a different object.          alert ($ (e). Data ("a");//Have you guessed the answer, the output is wwww; it's a little unexpected.          alert (e===w)//false          alert ($ (w). Data ("a"));//This is wwww;             // Use Jquery.data (Element,[key],[value]) to store data.          $.data (E, "B", "CCCC");//Save key-like data on E and W,          $.data (W, "B", "dddd");//  see if it will overwrite the front, although it is saved on a different object.          alert ($.data (E, "B"));//Should you be able to guess the answer, output cccc       &nbsP;  alert ($.data (W, "B"));//This output dddd                    });        });   </script >  

See the above example is not found that data ([Key],[value]) and Jquery.data (Element,[key],[value]) Two is not the same, right. Is there any relationship between them? How does data ([Key],[value]) overwrite the same value as the previous key?
Jquery.data (Element,[key],[value]) does not cause an overlay as long as it is bound to a different object. Is that so? Let's take a look at their source code.
See Jquery.data (Element,[key],[value]) source code first. JS Code:   jquery.extend ({       cache: {},           // Please use with caution       uuid:  0,          // unique for each copy of  jQuery on the page       // non-digits removed  to match rinlinejQuery       expando:  "JQuery"  +  (  jquery.fn.jquery + math.random ()  ). Replace ( /\D/g,  " ),           ....       data: function ( elem,  name, data, pvt /* internal use only */ )  {       //  whether data can be appended, not directly back to            if  ( !jquery.acceptdata ( elem )  )  {                return;            }              var privatecache,  thisCache, ret,                Jquery.expando This is a unique string that is generated when the jquery object is created.                internalKey =  jquery.expando,               getbyname  = typeof name ===  "string",                   //  must distinguish between the handling of DOM elements and JS objects, because ie6-7 cannot garbage-reclaim objects that are referenced across DOM objects and JS objects                 isnode = elem.nodetype,                    //  if it is a DOM element, use the global jquery.cache                  //  if it is a JS object, attach directly to the object                 cache = isNode ? jQuery.cache :  elem,                  //  Only defining an id for js objects if its cache already  exists allows               //  the code to shortcut on the same path as a DOM  node with no cache                id = isnode ? elem[ internalkey ] : elem[ internalkey ] & & internalKey,                isevents = name ===  "Events";                //  avoid doing more without having to work when trying to get data on an object that doesn't have any data           //  Object does not have any data, direct return       &n

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.