Drill down into a dense JavaScript array method _ Basics

Source: Internet
Author: User
Tags arrays javascript array

In JavaScript, arrays can be created using the array constructor, or created quickly using [], which is also the preferred method. An array is a prototype that inherits from object, and he has no special return value for TypeOf, and he only returns ' object '.

1. Array.prototype.slice method
The slice method of an array is often used to extract fragments from an array. However, it also has the ability to convert "class arrays" (such as arguments and htmlcollection) to true arrays.

Copy Code code as follows:

var Nodesarr = Array.prototype.slice.call (document.forms);
var Argsarr = Array.prototype.slice.call (arguments);

I wondered why the slice method of arrays had the ability to do it in a JavaScript engine. Slice's brother method is there such a skill?

With curiosity, download Google's V8 JavaScript engine source code to the local, V8 source download address: Https://github.com/v8/v8.

Find "Array.prototype.slice" in V8-master/src/array.js:

Copy Code code as follows:

function Arrayslice (start, end) {
Check_object_coercible (This, "Array.prototype.slice");
...
var result = []; That's the key.
if (End_i < start_i) return result;
if (usesparsevariant (array, Len, Is_array (array), end_i-start_i)) {
...
Sparseslice (Array, start_i, end_i-start_i, Len, result);
} else {
Simpleslice (Array, start_i, end_i-start_i, Len, result);
}
...
return result;

Then guess call "class array" to go should be the Simpleslice method, and then find "Simpleslice" in the source code, found Array.prototype.splice source code also called the Simpleslice method, and the result variable is also initialized to an empty array. However, to convert a "class array" to a true array by using the Splice method, you must pass in the starting position argument to 0, which is:

Copy Code code as follows:

var Nodesarr = Array.prototype.splice.call (document.forms, 0);

Because its implementation is to make the deleted array of items into a new array. Interested in the children's shoes can be seen under the Array.prototype.splice source code implementation.
In addition, slice can also clone an array:

Copy Code code as follows:

var arr = [1, 2, 3];
var Clonearr = Arr.slice (); Clonearr: [1, 2, 3]

2. Array.prototype.push method
Use the push method to merge arrays:

Copy Code code as follows:

var arr1 = [1, ' str ', {name: ' lang '}];
var arr2 = [2, ' ing '];
Array.prototype.push.apply (arr1, ARR2);
return result: [1, "str", {name: ' lang '}, 2, ' ing ']

3. Array.prototype.sort method
First code:

Copy Code code as follows:

var arr = [' 1 ', ' 2 ', ' 10 ', ' 12 '];
Arr.sort ();
return result: ["1", "10", "12", "2"]

The results above are usually not what we want, so how to sort by numerical size:

Copy Code code as follows:

Arr.sort (function (A, b) {
return a-b;
})
return result: ["1", "2", "10", "12"]

After you have the sort comparer function, you can customize many comparators to achieve personalized sorting.

4. Length Property
The length property of the array, not read-only, can also be written, such as using the Length property to truncate the array:

Copy Code code as follows:

var arr = [1, 2, 3, 4];
Arr.length = 2;
ARR: [1, 2]
arr.length = 0;
ARR: []

At the same time, if you make the length property larger, the length of the array increases, and the undefined is used as the new element to populate.

Copy Code code as follows:

var arr = [];
Arr.length = 3;
Arr: [Undefined, undefined, undefined]

Well, today is summed up here, has the early morning, after what new find append here.
Before, did not write the habit of blogging, only accustomed to the usual summary into the Youdao cloud notes, did not expect to write the point of view is really to spend a little thought, because to consider how to express, to let others better understand.

There is something wrong or understand the wrong place, but also hope that everyone help correct.

Also attach some common JavaScript array methods

The

Concat () connects two or more arrays and returns the result. The
Join () puts all the elements of an array into a string. element is delimited by the specified delimiter.
Pop () deletes and returns the last element of the array
push () adds one or more elements to the end of the array and returns a new length. The
reverse () reverses the order of the elements in the array.
Shift () deletes and returns the first element of the array
Slice () sorts the elements of the selected element
sort () array by returning an existing array
Splice () deletes the element and adds a new element to the array. The
Tosource () returns the source code for the object
ToString () converts the array to a string and returns the result. The
toLocaleString () converts the array to a local array and returns the result. The
Unshift () adds one or more elements to the beginning of the array and returns a new length.
valueof () returns the original value of the array object

Related Article

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.