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

Detailed explanation of the relationship between Array and Object in Javascript

黄舟
黄舟Original
2017-03-18 15:07:502066browse

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);

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);

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!

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