JavaScript Array Object Basic knowledge summary _javascript Tips

Source: Internet
Author: User
Tags javascript array
For the array object My summary thought is: 5 method, 3 strokes, 12 style

1. Statement 5 method: Only for one-dimensional arrays, and of course two-dimensional three-dimensional, here is no explanation
Copy Code code as follows:

var a = new Array ();
var a = new Array;
var a = new Array (10);//Create an Array object and specify the number of items in the array
var a = new Array ("Red", "Blue", "green");
var a = ["Red", "Blue", "green"];

2. Property 3 Recruit: Constructor,length,prototype
constructor represents the function that created the object. The constructor property is a member of all objects that have prototype. They include all of the JScript intrinsic objects except the Global and Math objects. The constructor property holds a reference to a function that constructs a particular object instance. For example:
Copy Code code as follows:

x = new String ("Hi");
if (X.constructor = = String)
To process (the condition is true).
Or
function MyFunc {
function body.
}
y = new MyFunc;
if (Y.constructor = = MyFunc)
To process (the condition is true).

Length indicates how long the array is, and how many entries it has. This does not write the example.
Prototype returns a reference to the object type prototype. Use the prototype property to provide a set of basic functions for the object's class. The operation of the new instance of the object, "inherit", gives the object a prototype.
For example, to add a method that returns the maximum element value in an array for the array object. To do this, declare the function, add it to the Array.prototype, and use it.
Copy Code code as follows:

function Array_max () {
var i, max = this[0];
for (i = 1; i < this.length; i++)
{
if (Max < this[i])
max = This[i];
}
return Max;
}
Array.prototype.max = Array_max;
var x = new Array (1, 2, 3, 4, 5, 6);
var y = X.max ();

After the code executes, Y saves the maximum value in the array x, or says 6.
3. Method 12-Type: Concat Method | Join Method | Pop Method | Push Method | Reverse Method | Shift Method | Unshift Method | Slice Method | Splice Method | Sort Method | ToString Method | ValueOf method
Copy Code code as follows:

1.concat (): Returns a new array, which is a combination of two or more arrays.
var a1 = [1,2,3,4];
var a2 = A1.concat ("5", ' 6 ');
alert (A2); Result: 1,2,3,4,5,6
2.join (): Returns a string value that contains all the elements of an array that is connected together, separated by the specified delimiter.
var a1 = [1,2,3,4];
var a2 = A1.join ();
var a3 = A1.join (",");
var a4 = a1.join ("|");
alert (A2); Result: 1,2,3,4
Alert (A3); Result: 1,2,3,4
alert (A4); Result: 1|2|3|4
3.pop (): Moves the last element in the divisor group and returns the element. If the array is empty, then the undefined is returned.
var a1 = [1,2,3,4];
var item = A1.pop ();
alert (item); Results: 4
alert (A1);//Result: 1,2,3
4.push (): Adds (appends) a new element to an array and returns the new length value of the array. The push method adds these elements in the order in which the new elements appear. If one of the arguments is an array, the array is added as a single element to the arrays. If you want to merge elements from two or more arrays, use the Concat method.
var a1 = [1,2,3,4];
A1.push (5);
A1.push ("6");
alert (A1);//Result: 1,2,3,4,5,6
5.reverse (): Returns an Array object in which the order of elements is reversed.
var a1 = [1,2,3,4];
A1.reverse ();
alert (A1);//Result: 4,3,2,1
6.shift (): Moves the first element in the divisor group and returns the element.
var a1 = [1,2,3,4];
A1.shift ();
alert (A1);//Result: 2,3,4
7.unshift (): Inserts the specified element into the starting position of the array and returns the array.
var a1 = [1,2,3,4];
A1.unshift (5);
alert (A1);//Result: 5,1,2,3,4
8.slice (): Returns a section of an array. A1.slice (start, [end]), the slice method is copied to the element that is specified by the finish, but does not include the element. If start is negative, it is the length + start processing, where length is the size of the array. If end is negative, it is treated as length + end where length is the length of the array. If End is omitted, the slice method is copied to the ending of arrayobj. If End appears before start, no elements are copied into the new array.
var a1 = [1,2,3,4];
var a2 = A1.slice (-1);
var a3 = A1.slice (0,-1);
var a4 = a1.slice (1);
alert (A2);//Result: 4
Alert (A3);//Result: 1,2,3
alert (A4);//Result: 2,3,4
9.splice (): Removes one or more elements from an array and, if necessary, inserts a new element at the position of the removed element, returning the removed element.
Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]
var a1 = [1,2,3,4];
var a2 = A1.splice (1,0);
var a3 = A1.splice (1,1);
var a4 = a1.splice (1, 1, "5");
alert (A2);//Result:
Alert (A3);//Result: 2
alert (A1);//Result: 1,5,4
10.sort (): Returns an Array object in which an element has been sorted.
var a1 = [2,3,1,4, "B", "a"];
var a2 = A1.sort ();
alert (A2);//Result: 1,2,3,4,a,b
11.toString (): Returns a string representation of the object.
var a1 = [1,2,3,4, "B", "a"];
var a2 = a1.tostring ();
alert (A2);//Result: 1,2,3,4,b,a
12.valueOf (): Returns the original value of the specified object.
var a1 = [1,2,3,4, "B", "a"];
var a2 = a1.valueof ();
alert (A2);//Result: 1,2,3,4,b,a

indexof Extensions:
Copy Code code as follows:

if (! ARRAY.PROTOTYPE.INDEXOF) Array.prototype.indexOf = function (item, i) {
I | | (i = 0);
var length = This.length;
if (i < 0) i = length + i;
for (; i < length; i++)
if (this[i] = = Item) return i;
return-1;
};
function Testindexof ()
{
var a =[1,2,3,4];
Alert (A.indexof (3));
}
<input type= "button" value= "Test get array Subscript" onclick= "testindexof ()"/>
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.