Array features of jquery objects

Source: Internet
Author: User
  1. 2.3Array features of jquery objects
  2. From the above section, we can see that jquery builds a function to complete the search, conversion, or other functions. The result is to find the elements, search, and search, which is just a way. You have to find a place to store the elements. This section is for analysis.
  3. The best place (SET) for storing ordered data in JS is array. So how can we implement arrays in jquery? You can use the following method:
  4. Jquery. FN. Prototype =NewArray ();
  5. In the previous sectionThisAdd to the. setarray (ARR) Function
  6. Array. Apply (This, Arr );
  7. If it is more perfect, add:
  8. Jquery. FN. Prototype. constructor = jquery.
  9. In this way, we inherit all the features of the array and can extend the Array Function in the jquery object. However, jquery does not use the inherited array to implement this internal set. It uses the implementation of array-like objects (see javascript: the definitive guide, 5th edition7.8).
  10. The object of the class array is still an object, just like an array. There is no big difference between arrays and objects. Ordered and unordered sets are different from each other. This difference is reflected in the Length attribute of the array. When an element is added, the relative number is automatically added. When an element is deleted, the relative number is automatically subtracted.
  11. Let's take a look at how jquery is implemented:
  12. // In the first case, handle $ (domelement) indicates a single Dom element, ignoring the context
  13. If(Selector. nodetype) {②
  14. This[0] = Selector;
  15. This. Length =1;
  16. Return This;
  17. }
  18. This is its first implementation method.This[0To directly set the DOM element at the first position, and set length =1. Here we can see that the object and the array both use the key/value pair form to exist in the object. The preceding JSON format is {0: Adom, length =1}. Here we will analyze the array in detail. The array inherits from the object. The final result of its [] interpretation analysis can be considered as an object in the {} structure. Index (such0.1,....) As the key of the object property. Use the values in the array as their corresponding values. Change the length value at the same time. That is why objects are essentially no different from arrays. In many source codes, for example, Yui uses objects to construct multidimensional arrays.
  19. This. Setarray (jquery. makearray (selector ));
  20. Is its second implementation method. The above implementation is a single element, a set of multiple elements of this implementation. It first calls the jquery. makearray (selector) Static Method to convert the Set (class array) into an array.
  21. The above analysis shows that both arrays and objects can use obj. [ATTR] to obtain the value corresponding to their key. For a set or an array of classes, the Length attribute must be implemented. With the Length attribute0~ Length-1You can get the corresponding value in the key attribute:
  22. // Convert the set of class arrays into an array. If it is a single element, an array of a single element is generated.
  23. Makearray: function (array ){
  24. VaR ret = [];
  25. If(Array! =Null) {Var I = array. length;
  26. // A single element, but the window, string, and function have the 'length' attribute. Add other judgments.
  27. If(I =Null| Array. Split | array. setinterval | array. Call)
  28. RET [0] = Array;
  29. Else// A set of class Arrays
  30. While(I) RET [-- I] = array [I];// Clone the Array
  31. }
  32. ReturnRET;
  33. },
  34. A standard array is generated. What will setarray do next?
  35. // Push all elements of the array-like object to the current jquery object.
  36. Setarray: function (elems ){
  37. This. Length =0;// Initialize the length, because the push will be in the original length ++
  38. Array. Prototype. Push. Apply (This, Elems );
  39. Return This;
  40. },
  41. This call calls array. Prototype. Push to automatically modify the Length attribute value (of course, an element is added ). As a result, many methods (such as shift) in the array can be seen as changing the value of length to the key/value pair of the object to complete unordered or re-ordered work. In fact, array and so on are implemented using C or C ++. However, the JS features it constructs allow us to think about how Javascript is implemented.
  42. The above setarray (elems) function only changes the set of the current jquery object and clears the previous elements in this object set. However, sometimes we want to save the elements in the original collection and perform jquery object operations on the new elements. It provides the pushstack function to create a new jquery object and save the reference of the original object. In this way, you may use the objects you want as needed:
  43. Pushstack: function (elems ){// Use jquery to construct a new object and reference the old object.
  44. VaR ret = jquery (elems );// Construct a new jquery object
  45. Ret. prevobject =This;// Save the reference of the old object
  46. ReturnRET;
  47. },
  48. The returned result is a newly built object with all the functions of the jquery object. You can also use prevobject to access the original old object.
  49. If a class array is built, you must provide some methods to operate on this set. operations on the set are nothing more than locating elements, searching elements, copying (slice), and deleting operations (SPLICE. Jquery also provides each and map extensions.
  50. These methods are only related to the set and are irrelevant to the set elements. Jquery provides many methods related to the element (DOM element.
  51. It provides two get element methods, get (INDEX) and eq (INDEX). The difference is that get is an element in the Set, while EQ is the clone of the returned element. The array is not modified. In fact, you can use [I] instead of get (I ). If get does not have a parameter, all elements are obtained.
  52. // Obtain the several DOM elements of the jquery object. No parameter exists, indicating all DOM elements.
  53. Get: function (Num ){
  54. ReturnNum = undefined? Jquery. makearray (This):This[Num];
  55. },
  56. // Obtain the nth element. The position of this element is counted from 0.
  57. EQ: function (I ){
  58. Return This. Slice (I, + I +1);
  59. },
  60. These two functions do not need to be analyzed. Next, let's take a look at how to locate elements in the collection:
  61. /Locate the location of elem in the jquery object (INDEX)
  62. Index: function (ELEM ){
  63. VaR ret =-1;
  64. ReturnJquery. inarray (// If the jquery object is used, the first element is used.
  65. ELEM & ELEM. jquery? ELEM [0]: ELEM,This);
  66. },
  67. // Determine the location (INDEX) Static Method of the ELEM element in the array
  68. Inarray: function (ELEM, array ){
  69. For(VAR I =0, Length = array. length; I <length; I ++)
  70. // Use === because on IE, window = Document
  71. If(Array [I] === ELEM)
  72. ReturnI;
  73. Return-1;
  74. },
  75. Inarray is the static method of jquery, while index is the function of positioning by calling inarray. The index function supports jquery objects or DOM elements, while inarray is a practical method that supports any element.
  76. Jquery provides methods such as the Server Load balancer replication function in the array, and a static merge method similar to Concat. Slice is implemented through slice in array:
  77. /Proxy array slice, the same operation.
  78. Slice: function (){
  79. Return This. Pushstack (array. Prototype. Slice. Apply (This, Arguments ));
  80. },
  81. It returns a new jquery object. The set of this object is the set to be copied. For merge, It is a static method that adds the second element to the array of the first parameter.
  82. // Append the second element to the first array.
  83. Merge: function (first, second ){
  84. // We have to loop this way because IE & opera overwrite the length
  85. // Expando of getelementsbytagname
  86. VaR I =0, ELEM, Pos = first. length;
  87. // Also, we need to make sure that the correct elements are being
  88. // Returned (ie returns comment nodes in a '*' query)
  89. If(Jquery. browser. MSIE ){
  90. While(ELEM = Second [I ++])
  91. If(ELEM. nodetype! =8)
  92. First [POS ++] = ELEM;
  93. }Else
  94. While(ELEM = Second [I ++])
  95. First [POS ++] = ELEM;
  96. ReturnFirst;
  97. },
  98. Jquery's each performs a callback function for each element in the set.
  99. // The callback (index, ELEM) function is executed for each element of the current jquery object.
  100. Each: function (callback, argS ){// Return this
  101. // It calls the jquery static method. Mothodize in prototype is a good solution to this type of problem.
  102. ReturnJquery. Each (This, Callback, argS );
  103. },
  104. It calls the jquery. Each static method to complete the function:
  105. // Execute the callback function for each object. ARGs is only used internally
  106. Each: function (object, callback, argS ){
  107. VaR name, I =0, Length = object. length;
  108. // It is similar to else processing. The args parameter passing replaces the object attribute value.
  109. If(ARGs ){
  110. If(Length = undefined ){
  111. For(Name in object)
  112. If(Callback. Apply (object [name], argS) =False)
  113. Break;
  114. }Else
  115. For(; I <length ;)
  116. If(Callback. Apply (object [I ++], argS) ====False)
  117. Break;
  118. }Else{
  119. // Not an array-like object. Call the callback function for each attribute.
  120. If(Length = undefined ){
  121. For(Name in object)
  122. If(Callback. Call (object [name], name, object [name]) =False)
  123. Break;
  124. }Else
  125. // Array-like object, which is processed as an array
  126. For(VaR value = object [0]; I <length & callback. Call (value, I, value )! =False; Value = object [++ I]) {}
  127. }
  128. ReturnObject;
  129. },
  130. This static method supports the class array (array) or object of the first parameter. Is an array to call back each element. If it is an object, the callback operation is performed on each attribute value. The format of this callback function is as follows: callback: function (index, value ). Index is the index number, and value is the index processing attribute of the element or object corresponding to the index of the array. If the ARGs parameter is used, the callback function format is as follows: callback: function (ARGs ). ARGs sets parameters for the callback function. Let's take a look at the each of the jquery object. Its second parameter, argS, uses the input ARGs to directly set parameters for callback, instead of raising the index and corresponding elements in the set by default, but the number of executions is still the length of the set.
  131. Jquery map converts a group of elements into other arrays (composed of callback function return values)// Convert a group of elements into other arrays and then construct a new jquery object based on the array ..
  132. Map: function (callback ){
  133. Return This. Pushstack (jquery. Map (This, Function (ELEM, I ){
  134. ReturnCallback. Call (ELEM, I, ELEM );
  135. }));
  136. },
  137. This function first uses jquery. Map (This, Function (ELEM, I) {}ThisEach element of the jquery object set is uploaded to the callback function as the ELEM parameter of the callback function. The callback function executes the callback function in the MAP: function (callback) of the instance method, that is, the callback in jquery. MAP is only the function of passing the parameter proxy. Jquery. Map gets the converted element set through the callback of the proxy.
  138. The next step is to use pushstack to construct the elements in this set into a new jquery object and return it, and save the reference of the original jquery object.
  139. Let's take a look at the static method of MAP:
  140. // Return the set of return values of the callback function that operates on each element of elems.
  141. Map: function (elems, callback ){
  142. VaR ret = [];
  143. For(VAR I =0, Length = elems. length; I <length; I ++ ){
  144. VaR value = callback (elems [I], I );
  145. If(Value! =Null)// Indicates that the number of converted sets may be less than that of the original set.
  146. RET [ret. Length] = value;
  147. }
  148. ReturnRet. Concat. Apply ([], RET );// Implemented using array Concat
  149. }
  150. the static method of map returns an array composed of elements returned by each callback.

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.