JavaScript Array Array Basics Introduction _ Basics

Source: Internet
Author: User
Tags array length javascript array

Javascript is a magical language, and its array is just as unique. We should go to its dross, take its essence, summed up the common best practice. If there are errors, please point out.

A JavaScript array is an object of an array of classes that has attributes of an object. When property names are small and contiguous integers, you should use an array, otherwise, use the object.

Array source

All arrays are constructed out of array, so let's test constructor this property.

var arr = [];
Arr.constructor = = Array; True
Arr.constructor = = Array.prototype.constructor;//True

Create an array

Array literal method
var arr1 = [1, 2, 3];//[1,2,3]

//constructor mode
var arr2 = new Array ();    [] Empty array
var arr3 = new Array (' 9 ');   ["9"] a string element
var arr4 = new Array (9);    [] The length of the array is 9
var arr5 = new Array ([9]);   [[9]] equivalent to two-D array
var arr6 = new Array (1, 2, 3);//[1, 2, 3]
var arr7 = new Array (1, function f () {}, {o:6}, Nu Ll,undefined,true);
Arrays can store any mixed data types

Because of the Arr4 method, when only one numeric parameter is passed into the array's constructor, the constructor returns an empty array with the Length property set. So it is recommended to use an array literal method, short and concise.

Detects if an object is an array

var arr1 = [1, 2, 3];
typeof (ARR1); Object

As we all know, typeof does not detect types correctly.

arr1 instanceof Array; True

Instanceof Way in a Web page is no problem, once nested other Web pages, there will be two global scope, the detection of each other when the call will be problematic.

Array.isarray (ARR1); True

Array.isarray () is a new method of ECMAScript5, without defects. The only problem is that the IE8 browser is not supported, and IE9 browsers are not supported in strict mode.

Object.prototype.toString.apply (arr1). Slice (8,-1); Array

The final approach is the best way to detect types.

Array length

The length of an array is also its property, and increasing length does not have a cross-border error.
The length value is equal to the largest integer property name plus 1 for the array.

var arr1 = [];
ARR1[9] = 1; A length of 10, containing only an array of elements

Setting a decimal value deletes the attribute with the property name greater than or equal to length.
Setting the length value to 0 is equivalent to emptying the array.

var arr2 = [1, 2, 3, 4, 5];
Arr2.length = 3; [1, 2, 3]
arr2.length = 0;//[]

Array traversal

Traversing an array does not use a for in loop to traverse an array, because for-in will traverse all the properties on the prototype chain, but we don't need so much. The recommended way to use a for loop.

var arr1 = [1, 2, 3];
Arr1.test = 9;

For in way
for (Var prop in arr1) {
  cosole.log (prop, Arr1[prop]);
The output
is as follows//0 1
//1 2
//2 3
//test 9

//for cycle mode
for (var i = 0, len = arr1.length; I < le N i++) {
  console.log (arr1[i]);
}
The output is as follows
//1
//2
//3

We see a test value in the for in way, which can be excluded by using the hasOwnProperty function, but that's a lot slower than the for-loop approach.
Caching array lengths is a necessary step, with performance overhead per visit (the latest browsers are optimized for this).

Summary

A brief introduction of the relevant basics of array, here is to be able to have a more comprehensive understanding of the array. The next article introduces the array method.

JavaScript Although there are a lot of difficult to understand the place, with a long time to learn, I have slowly fell in love with it (because now no sister let Me Love).

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.