<span id="Label3"></p><p><p>Jquery.data is the data caching system of JQUERY. Its main function is to add data to ordinary objects or DOM Elements.</p></p>1 Internal Storage principle<p><p></p></p> <p><p>This principle is simple, the data originally to be added to the DOM element itself is now centrally stored in the cache Collection. They are linked by a numeric key starting from 1. This way, DOM elements will not be as cumbersome as they used to be, and there will be no memory leaks caused by previous circular References. Now the DOM just needs to save this numeric key Value. This property value is stored in a property of the Dom element, which is generated by jquery.expando.</p></p>2 Data constructors<pre><pre><span style="color: #0000ff"></span>This. cache = {}, 0<span style="color: #000000">, { </span><span style="color: #0000ff">function</span><span style="color: #000000">() { </span><span style="color: #0000ff">return</span> <span style="color: #000000"> {};} });</span></pre></pre><p><p>First look at the Object.defineproperty function, which works by adding attributes to an object, or modifying an attribute of an existing Property. Let's take a look at the properties in ECMAScript5 First.</p></p>2.1 Properties in ECMAScript5<p><p>There are two types of properties in ECMAScript5: data properties and accessor Properties.</p></p>2.1.1 Data properties:<p><p>The Data property contains the position of a data value, where the value can be read and Written. There are 4 features to limit their behavior.</p></p><p><p><b>①[[configurable]]</b></p></p><p><p>Can I delete an attribute through delete, can I modify the attribute of the property, and change the property to an accessor property? The default is True.</p></p><pre><pre><span style="color: #0000ff">var</span> Obj={name: "abc"<span style="color: #000000">};object.defineproperty (obj,</span>"name"<span style="color: #000000">, { configurable:</span><span style="color: #0000ff">false</span><span style="color: #000000">}); Console.log (obj.name);</span> <span style="color: #0000ff">Delete</span> <span style="color: #000000">obj.name;console.log (obj.name); object.defineproperty (obj,</span>"name"<span style="color: #000000">, { configurable:</span> <span style="color: #0000ff"> True</span><span style="color: #000000">});</span></pre></pre><p><p>Note that once the <b>configurable</b> is set to <b>false</b>, it is no longer set Back. And there will be an Error.</p></p><p><p><b>②[[enumerable]]</b></p></p><p><p>Whether the for-in loop can get the Property. The default is True. ECMAScript specifies that this attribute is true for a property defined by a programmer.</p></p><p><p><b>③[[writable]]</b></p></p><p><p>Whether the value of the property can be Modified. The default is True.</p></p><pre><pre><span style="color: #0000ff">var</span> Obj={name: "abc"<span style="color: #000000">};object.defineproperty (obj,</span>"name"<span style="color: #000000">, { writable:</span><span style="color: #0000ff">false</span><span style="color: #000000">}); Console.log (obj.name); obj.name</span>= "111"<span style="color: #000000">; console.log (obj.name)</span> ;</pre></pre><p><p><b>④[[value]]</b></p></p><p><p>The location where the property value Resides. It is obtained when the value of the property is read, and it is written here when the property value is Written.</p></p><pre><pre><span style="color: #0000ff">var</span> Obj={name: "abc"<span style="color: #000000">};console.log (obj.name); object.defineproperty (obj,</span>"name"<span style="color: #000000">, { value:</span>111 <span style="color: #000000">}); console.log (obj.name);</span></pre></pre>2.1.2 Accessor Properties<p><p>The accessor property does not contain the actual property value, it contains two functions (getter and Setter)</p></p><p><p><b>①[[get]]</b></p></p><p><p>The function that is called when the property is Read. Default is undefined</p></p><p><p><b>②[[set]]</b></p></p><p><p>The function that is called when the property is Written. Default is undefined</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">var</span></span>obj=<span style="color: #000000"><span style="color: #000000">{};object.defineproperties (obj,{name:{get:</span></span><span style="color: #0000ff"><span style="color: #0000ff">function</span></span><span style="color: #000000"><span style="color: #000000"> () { </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span>obj["_name"<span style="color: #000000"><span style="color: #000000">]; }, set:</span></span><span style="color: #0000ff"><span style="color: #0000ff">function</span></span><span style="color: #000000"><span style="color: #000000">(name) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(name! = "c #"<span style="color: #000000"><span style="color: #000000">) {obj[</span></span>"_name"]=<span style="color: #000000"><span style="color: #000000">name; }}, label:{get:</span></span><span style="color: #0000ff"><span style="color: #0000ff">function</span></span><span style="color: #000000"><span style="color: #000000"> () { </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span>"you Can't change me!!!" "<span style="color: #000000"><span style="color: #000000">; }}); Obj.name</span></span>= "JavaScript"<span style="color: #000000"><span style="color: #000000">; Console.log (obj.name); Obj.label</span></span>= "i want to change you!!!" "<span style="color: #000000"><span style="color: #000000">; Console.log (obj.label);</span></span></pre><p><p>The above gives the cache a property of 0, giving only the get Property. therefore, You cannot assign a value to this Property. can only be Obtained.</p></p><pre><pre><span style="color: #0000ff"></span>This. expando = Jquery.expando + math.random ();</pre></pre><p><p>This expando is the key that is used to store the DOM element or object in the Cache. That is, it will be stored as a property of the DOM Element.</p></p><p><p></p></p><p><p>The property name inside the red frame is generated by the above statement, indicating that the Div1 property is stored in the Cache's 1 Attribute.</p></p><pre><pre>Data.uid = 1;</pre></pre>This property indicates that the Cache's properties will increment from 1 onwards. Since 0 is already occupied by this frozen empty json, it starts from 1. The next time we add a property to a DOM element, it will be saved in the Cache's 2 Property. 3 allowed elements of the added property<pre><pre><span style="color: #0000ff">function</span> <span style="color: #000000">(owner) { </span><span style="color: #0000ff">return</span> owner.nodetype? <span style="color: #000000"> </span> <span style="color: #0000ff">true</span> <span style="color: #000000">;};</span></pre></pre><p><p>This piece of code is very able to write. Indicates that if owner is a DOM element, only Element_node and Document_node two elements can add attributes, and if owner is an object, you can add Attributes. 4 Prototype Properties 4.1 key:function (owner) {</p></p><pre><pre><span style="color: #0000ff">if</span> ( ! <span style="color: #000000">data.accepts (owner)) { </span><span style="color: #0000ff">return</span> 0<span style="color: #000000">;}</span></pre></pre><p><p>If owner cannot be added to data, the No. 0 element of the cache is Returned.</p></p><pre><pre><span style="color: #0000ff"></span>This. expando];</pre></pre><p><p>This is what we get here.</p></p><p><p></p></p><p><p>If this attribute can be found from owner, it has previously been added to the value, i.e. it already has a key in the Cache.</p></p><pre><pre><span style="color: #0000ff">if</span> ( ! <span style="color: #000000">Unlock) { </span>= data.uid++<span style="color: #000000">; </span> <span style="color: #0000ff">Try</span> <span style="color: #000000">{ </span>this. expando]<span style="color: #0000ff"></span>=<span style="color: #000000"> {value:unlock}; Object.defineproperties (owner, descriptor); </span> <span style="color: #0000ff">Catch</span> <span style="color: #000000">(e) { </span>this. expando]<span style="color: #0000ff"></span>=<span style="color: #000000"> unlock; Jquery.extend (owner, descriptor);} }</span></pre></pre><p><p>If it is not found, the first time you add a property to owner, you create a key. This key is based on the UID of data plus 1.Data uid is a static Property. This will be able to record the key of the previous element is how much, this time the key should be how Much. Inside the try block, add its index to the cache for owner (that is, DOM element or object). There is a compatibility issue Here. Security issues arise when the Android system <4, so jquery uses the Extend static method to extend descriptor to the Owner.</p></p><pre><pre><span style="color: #0000ff">if</span> ( ! <span style="color: #0000ff"></span> this <span style="color: #000000">. cache[unlock]) { </span>This. cache[unlock] =<span style="color: #000000"> {};}</span> <span style="color: #0000ff"></span></pre></pre><p><p>This assigns an object to the key that owner corresponds to in the Cache.</p></p><p><p>The function of this key method is to create a property for the DOM element and assign a value (an empty Object) to the corresponding key in the Cache.</p></p><p><p></p></p><p><p>Key returns the unlock, which is the owner Object's corresponding key in the Cache. 4.2 set:function (owner, data, Value) {</p></p><pre><pre><span style="color: #0000ff"></span>this. cache[unlock];<span style="color: #0000ff"></span><span style="color: #000000"></span></pre></pre>The first sentence does not have to explain, the second sentence or CacheKey Value. As shown, the cache here is currently {} because no attributes have been added to Div1 before.<pre><pre><span style="color: #0000ff">if</span> <span style="color: #0000ff">typeof</span> data = = = "string"<span style="color: #000000"> ) { </span>= value;</pre></pre><p><p>Here's What our code is going to Do:</p></p><pre><pre>$ ("#div1"). Data ("name", "div1");</pre></pre><p><p>But we have a lot of properties to set,</p></p><pre><pre>$ ("#div1"). Data ({name: "div1", from: "0", to: "100"});</pre></pre><p><p>jquery is handled in such a way that:</p></p><pre><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Handle: [owner, {properties}] args</span></span>}<span style="color: #0000ff"><span style="color: #0000ff">Else</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Fresh assignments by object is shallow copied</span></span> <span style="color: #0000ff"><span style="color: #0000ff">if</span></span><span style="color: #000000"><span style="color: #000000">(jquery.isemptyobject (cache)) {jquery.extend (</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">. cache[unlock], data); </span></span><span style="color: #008000"><span style="color: #008000">//</span></span><span style="color: #008000"><span style="color: #008000">Otherwise, Copy the properties one-by-one to the cache object</span></span>}<span style="color: #0000ff"><span style="color: #0000ff">Else</span></span><span style="color: #000000"><span style="color: #000000"> { </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> for</span>(prop<span style="color: #0000ff"><span style="color: #0000ff">inch</span></span><span style="color: #000000"><span style="color: #000000">Data) {cache[prop]</span></span>=<span style="color: #000000"><span style="color: #000000">data[prop]; } }}</span></span></pre><p><p>In fact, here personally think no longer judge, because extend inside is also with a for loop to extend the properties of data to the Cache.</p></p>4.3 get:function (owner, Key) {<pre><pre><span style="color: #0000ff">var</span> <span style="color: #0000ff"></span> this <span style="color: #0000ff"></span> this <span style="color: #000000">. Key (owner)];</span> <span style="color: #0000ff">return</span> key = = = Undefined? cache:cache[key];</pre></pre><p><p>Gets the property, the code is very simple. Returns the cache object if key does not exist.</p></p>4.4 access:function (owner, key, Value) {<p><p>This is a uniform access to the get and set METHODS. 4.5 Remove:function (owner, Key) {</p></p><pre><pre><span style="color: #0000ff"></span>this. cache[unlock];<span style="color: #0000ff"></span><span style="color: #000000"></span></pre></pre><p><p>Gets the Owner's data object in the cache, This.key returns the Owner's key in the Cache.</p></p><pre><pre><span style="color: #0000ff">if</span> (key = = =<span style="color: #000000"> undefined ) {</span>this. cache[unlock] = {};<span style="color: #0000ff"></span></pre></pre><p><p>If you do not specify that you want to delete that attribute, jquery removes all data attributes from Owner.</p></p><p><p>Otherwise the key is not an array,</p></p><pre><pre><span style="color: #0000ff">if</span> <span style="color: #000000">(jquery.isarray (key)) { </span>=<span style="color: #000000"> key.concat (key.map (jquery.camelcase));}</span></pre></pre>is an array, the key array is merged with the array of each of the Key's humps, namely:<pre><pre>$ ("#div1"). Remove (["one_key", "two_key"]);</pre></pre><p><p>After the above code, key is changed to <b>["one_key", "two_key"</b><b></b>,<b>"oneKey", "twokey"</b><b>]</b>. These 4 properties will be removed later.</p></p><p><p>If the key is not an array:</p></p><pre><pre>Camel =<span style="color: #000000"> jquery.camelcase (key);</span> <span style="color: #0000ff">if</span> <span style="color: #0000ff">inch</span> <span style="color: #000000">Cache) { </span>=<span style="color: #000000"></span><span style="color: #0000ff">else</span> <span style="color: #000000"> { </span>=<span style="color: #000000"> camel; </span> <span style="color: #0000ff"></span> in cache? <span style="color: #000000"> </span>|| <span style="color: #000000"> [] );}</span></pre></pre><p><p>The same first to lose the hump, and then determine whether the key exists, does not exist to determine the presence of the hump form key, followed by the regular expression used to remove the hump form before and after the Space.</p></p><p><p></p></p><p><p>When these conditions have been filtered out, Delete operations:</p></p><pre><pre>i =<span style="color: #000000"> name.length;</span> <span style="color: #0000ff"></span> while (i--<span style="color: #000000"> ) { </span><span style="color: #0000ff">Delete</span> <span style="color: #000000"> cache[name[i];}</span></pre></pre><p><p>Use Delete in the while loop to Delete. 4.6 hasdata:function (owner) {</p></p><p><p>Whether the cache has Woner data objects. 4.7 discard:function (owner) {</p></p><p><p>Deletes the Woner data object in the Cache. 5 Creating two private caches</p></p><pre><pre><span style="color: #0000ff">New</span><span style="color: #000000"></span><span style="color: #0000ff">new</span> Data ();</pre></pre>so, We can't get this cache directly outside of Jquery. Because it is a local variable of jquery. Data_user is intended for use by developers and Data_priv for use within jquery. 6 Creating external interfaces (tool methods and prototyping Methods)<p><p>Because the data constructor is jquery private, We cannot access it outside, so the previous methods, we can not directly access, jquery here, gives us some interfaces. To manipulate data_user, which attributes the DOM element and Object. The tool method is very simple just encapsulation of the Data_user Method. We mainly look at the prototype Method.</p></p><pre><pre><span style="color: #000000">jQuery.fn.extend ({ </span><span style="color: #0000ff">function</span>(key, Value) {</pre></pre><p><p>Setting values and values will go to the above method.</p></p><p><p>Here is the idea that if you set a value, you set values for all options in the collection, and if you get the value, only the value of the first Option.</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(key = = =<span style="color: #000000"><span style="color: #000000">Undefined) { </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span><span style="color: #000000"><span style="color: #000000">. Length) {data</span></span>=<span style="color: #000000"><span style="color: #000000">Data_user.get (elem); </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(elem.nodetype = = = 1 &&!data_priv.get (elem, "hasdataattrs"<span style="color: #000000">) <span style="color: #000000">) {attrs</span></span>=<span style="color: #000000"><span style="color: #000000">elem.attributes; </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> for</span>(; I < attrs.length; i++<span style="color: #000000"><span style="color: #000000">) {name</span></span>=<span style="color: #000000"><span style="color: #000000">attrs[i].name; </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(name.indexof ("data-") = = = 0<span style="color: #000000"><span style="color: #000000">) {name</span></span>= Jquery.camelcase (name.slice (5<span style="color: #000000"><span style="color: #000000">) ); Dataattr (elem, name, data[name]); }} Data_priv.set (elem,</span></span>"hasdataattrs",<span style="color: #0000ff"><span style="color: #0000ff">true</span></span><span style="color: #000000"><span style="color: #000000"> ); } } </span></span><span style="color: #0000ff"><span style="color: #0000ff">return</span></span><span style="color: #000000"><span style="color: #000000">data;}</span></span></pre><p><p>In line 3rd, the elem corresponding property in the cache has been taken, and the following jquery is added to Elem by adding all the attributes from the Elem Attributes.</p></p><p><p>the <b>Hasdataattrs</b> property is added by ourselves because the following code needs to be executed only once. Line 6th of the code, get all the properties of elem;</p></p><p><p></p></p><p><p>This namednodemap type, we seldom use it directly, and it is "dynamic" like Nodelist,htmlcollection. Each item in the NamedNodeMap collection is of type attr, and the Attr object has 3 properties: name,value and Specified.</p></p><p><p>In the following for loop, it detects if the <b>"data-"</b> prefix is included in the name Attribute. Some words will remove it, and the rest to the hump form.</p></p><p><p>If key does not exist, it means getting all of the property values for the first option in the Anthology. Elem represents the first Option.</p></p><pre><pre><span style="color: #0000ff">if</span> <span style="color: #0000ff">typeof</span> Key = = = "object"<span style="color: #000000"> ) { </span><span style="color: #0000ff">return</span><span style="color: #0000ff"></span>this. each (<span style="color: #0000ff">function</span><span style="color: #000000">() { </span> <span style="color: #0000ff"></span>This<span style="color: #000000">, key); });</span></pre></pre>Corresponds to this form:<pre><pre>$ ("#div1"). Remove (["one_key", "two_key"]);</pre></pre><p><p>Jquery.data Source</p></p></span>
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