Home  >  Article  >  Web Front-end  >  Some weird behaviors of JavaScript arrays_javascript tips

Some weird behaviors of JavaScript arrays_javascript tips

WBOY
WBOYOriginal
2016-05-16 15:18:301308browse

The importance of arrays in programming languages ​​is self-evident. Arrays in JavaScript are also one of the most commonly used objects. Arrays are ordered collections of values. Due to weak types, arrays in JavaScript are very flexible and powerful. Unlike arrays in strongly typed high-level languages ​​such as Java, which can only store elements of the same type or its subtypes, JavaScript can store multiple types of elements in the same array, and the length can also be dynamically adjusted, as the data increases or Reduce automatic changes to array length.

Today, I reviewed JavaScript arrays, and then summarized some of his weird behaviors. I will share them with you here. If there are any mistakes, please point them out!

Strange 1: The Array() constructor function can be called without using the new keyword:

The Array() constructor uses the parameters passed to him as elements of the array to create an array. Generally, we call it as follows:

var a = new Array(1, 2, "bom!");
a.length; //3
console.log(a); //[1, 2, "bom!"] 

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

var a = Array(1, 2, "bom!");
a.length; //3
console.log(a); //[1, 2, "bom!"] 

Although I don’t know what its internal implementation mechanism is, I guess that its constructor function may be defined as follows:

function Array(args) {
//如果,this不是Array的实例的话,
//说明不是通过new调用的,则在这里再重新调用
if( !this instanceof Array) {
return new Array(args);
}//后面是正常调用时的实现代码<br />//...<br />} 

Strange 2: When only one parameter is passed to the constructor, the behavior is unpredictable

If only one parameter is passed, and this parameter is an integer, an array will be obtained, and the length is equal to this parameter

var a = new Array(12);
console.log(a.length); //12
console.log(a); //[] 

If you only pass a floating point number, an error will be reported:

var a = new Array(1.1); //Uncaught RangeError: Invalid array length(…) 

Passing a string will work fine, with the string as the first element of the array:

var a = new Array("1.1");
console.log(a.length); //
console.log(a); //["1.1"] 

But in order to avoid ambiguity, I suggest that it is best to create the array directly in the form of literals:

var a = []; //空数组
var a = [1, 1, "bom"]; //三个元素
var a = [12]; //一个元素,并且元素是12 

Strange 3: The length attribute of the array can be modified (writable)

As follows, we directly changed the length from 2 to 100, and the modification was successful! ! !

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

Although length is equal to 100, elements a[4]-a[99] do not exist, and if you request their values, for example, do it in a loop from 0 to a.length, then will get undefined.

Watch next:

var a = [1, 2, 3, 4];
a.length = 100;
console.log(a[10]); //undefined
console.log(99 in a); //false 

Somewhat similar to the example below:

var a = [1, 2, 3, 4];
a[99] = undefined;
console.log(a.length); //100 

The difference is that a[99] here exists because we created it, even though it has an undefined value. But all elements from a[4] to a[98] do not exist, as follows:

var a = [1, 2, 3, 4];
a[99] = undefined;
console.log(99 in a); //true;
console.log(98 in a); //false
console.log(a.length); //100 

The above has shared with you some strange behaviors of JavaScript arrays. Please forgive me if the article is not well written, thank you!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn