Home > Web Front-end > JS Tutorial > Detailed explanation of the relationship between Array and Object in Javascript

Detailed explanation of the relationship between Array and Object in Javascript

黄舟
Release: 2017-03-18 15:07:50
Original
2109 people have browsed it

In

Javascript, Array is also Object, but some people may mistakenly believe that access in the form a['key'] is Array. Function, actually not.

In the following example, after Array is assigned a value, its length property is still 0, because the data is not placed in the array position, but is placed in the Array's prototype object Object.

var a = new Array();
a['one'] = 1;
a['two'] = 2;
a['three'] = 3;

alert(a.length);
Copy after login

Another important point:

When javascript instantiates an object, you can create the object in the form {key:value}. The key can be a direct quantity or a string. In the following example, the contents of a1 and a2 are exactly the same.

It should be noted that var1 of a1 or a2 will not be replaced with 'pm'.

var var1 = 'pm';
var a1 = {var1:'test is ok',var2:'here'};
alert(a1.var1);
Copy after login

The above is the detailed content of Detailed explanation of the relationship between Array and Object in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template