Home >Web Front-end >JS Tutorial >Interesting JavaScript array length problem code description_javascript skills

Interesting JavaScript array length problem code description_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:11:20892browse
First point:
Copy code The code is as follows:

var lennon =Array();
lennon["name"]="windy";
lennon["year"]="1989";
lennon["living"]=true;
alert( lennon.length);

Result: lennon.length is 0

Cause:
Increasing array elements in the following way will not change the length of the array
arr["a"]=3;
Second point:
Copy code Code As follows:

var foo2 = [];
foo2['3'] = 3;
foo2['age'] = 20;
debugger;
alert (foo2.length);


foo2.length is 4 again

All array objects without assignment will be defined as undefined
JavaScript can automatically resize arrays. This automatic resizing mechanism makes out-of-bounds arrays more likely to occur and harder to detect. So pay extra attention to situations like this.
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