Research on the Javascript Array Method

Source: Internet
Author: User

Research on the Javascript Array Method
1. The slice Method of Array. prototype. slice Method Array is usually used to extract fragments from an Array. However, it also has the ability to convert "class arrays" (such as arguments and HTMLCollection) into real arrays. 1 var nodesArr = Array. prototype. slice. call (document. forms); 2 3 var argsArr = Array. prototype. slice. call (arguments); I wondered why the slice Method of the array has such a skill and how it is implemented in the javascript engine? Is slice's sibling method capable of doing this? With curiosity, download Google's V8 javascript Engine source code to your local device. For V8 source code: https://github.com/v8/v8. In v8-master/src/array. search for "Array. prototype. slice ": 1 function ArraySlice (start, end) {2 CHECK_OBJECT_COERCIBLE (this," Array. prototype. slice "); 3... 4 var result = []; // This sentence is critical 5 6 if (end_ I <start_ I) return result; 7 8 if (UseSparseVariant (array, len, IS_ARRAY (array ), end_ I-start_ I) {9... 10 SparseSlice (array, start_ I, end_ I-start_ I, len, result); 11} else {12 SimpleSlice (array, start_ I, End_ I-start_ I, len, result); 13} 14... 15 return result; then I guess the SimpleSlice method should be used to call the "class Array". Then I find "SimpleSlice" in the source code and find the Array. prototype. the SimpleSlice method is also called in the splice source code, and the result variable is also initialized as an empty array. However, to use the splice method to convert a "class Array" to a real Array, you must input the starting position parameter 0, that is, var nodesArr = Array. prototype. splice. call (document. forms, 0); because its implementation principle is to make the deleted array items into a new array. For more information, see the source code implementation of Array. prototype. splice. In addition, slice can clone an array: 1 var arr = [1, 2, 3]; 2 var cloneArr = arr. slice (); // cloneArr: [1, 2, 3] 2. array. prototype. the push method can use the push method to merge Arrays: 1 var arr1 = [1, 'str', {name: 'lang '}]; 2 var arr2 = [2, 'in']; 3 Array. prototype. push. apply (arr1, arr2); 4 // return result: [1, "str", {name: 'lang '}, 2, "ing"] 3. array. prototype. run the following code in the sort method: var arr = ['1', '2', '10', '12']; arr. sort (); // return result: ["1", "10", "12", "2 "] This is often not what we want, so how to sort by value: arr. sort (function (a, B) {return a-B;}) // return result: ["1", "2", "10 ", "12"] With the sorting comparator function, you can customize many comparator to achieve personalized sorting. 4. the length attribute of the length attribute array is not read-only. It can also be written. For example, the length attribute is used to truncate the array: 1 var arr = [1, 2, 3, 4]; 2 arr. length = 2; 3 // arr: [1, 2] 4 arr. length = 0; 5 // arr: [] At the same time, if the length attribute is increased, the length value of the array increases and the undefined attribute is used as the new element. 1 var arr = []; 2 arr. length = 3; 3 // arr: [undefined, undefined, undefined]

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.