JavaScript array of some wonderful behavior _javascript tips

Source: Internet
Author: User

The importance of arrays in programming languages is self-evident, and in JavaScript, arrays are one of the most commonly used objects, arrays are ordered collections of values, and because of the weak type, the arrays in JavaScript are flexible and powerful, It's not like a Java. Strong-type advanced language arrays can hold only the same type or its subtype elements, JavaScript may hold multiple types of elements in the same array, and can be dynamically adjusted in length, which can be changed as the data increases or decreases the automatic array length.

Today, review the JavaScript array, and then, some of his wonderful behavior summed up, here and everyone share, if there is a wrong place, welcome to point out!

Exotic Flowers 1:array () constructor functions can be called without using the New keyword:

The array () constructor uses the arguments passed to him as an element of the array to create the arrays, which, in general, are called as follows:

var a = new Array (1, 2, "bom!");
A.length; 3

However, it is also possible to omit new, as follows:

var a = Array (1, 2, "bom!");
A.length; 3

Although, I don't know what his internal implementation mechanism is, however, suppose that his constructor function might be defined as follows:

function Array (args) {
//If, this is not an instance of Array,
//description is not invoked through new, then call back
if (!this instanceof Array) return
new Array (args);
} Following is the implementation code for the normal call
//...

Miracle 2: Behavior is unpredictable when only one parameter is passed to the constructor

If you pass only one argument, and this argument is an integer, you get an array, and length equals this argument

var a = new Array (a);
Console.log (a.length); 12

If only one floating-point number is passed, an error occurs:

 
 

Passing a string works, and the string acts as the first element of the array:

var a = new Array ("1.1");
Console.log (a.length); //

But to avoid ambiguity, I suggest it is best to create an array directly using the literal form:

var a = []; Empty array
var a = [1, 1, BOM];//three elements

Miracle 3: The length property of the array can be modified (writable)

as follows, we directly changed the length of 2 to 100, but also modified the success of the!!!

var a = [1, 2, 3, 4];
Console.log (a.length); 4
a.length = 100; 

Although length equals 100, the element a[4]-a[99] does not exist, and if you ask for their value, for example, from 0 to A.length doing a loop, then you get undefined.

Next look:

var a = [1, 2, 3, 4];
A.length = m;
Console.log (a[10]); Undefined

A bit like the following example:

var a = [1, 2, 3, 4];
A[99] = undefined;

The difference is that the a[99 here exists because we created it, even though he had a undefined value. However, all elements from a[4] to a[98 are not present, as follows:

var a = [1, 2, 3, 4];
A[99] = undefined;
Console.log (in a); true;
Console.log (in a); False

The above mentioned to share the JavaScript array of some of the wonderful behavior, the article is not written well forgive me, thank you!

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.