Home  >  Article  >  Web Front-end  >  有趣的javascript数组定义方法_基础知识

有趣的javascript数组定义方法_基础知识

WBOY
WBOYOriginal
2016-05-16 18:19:53975browse
复制代码 代码如下:

 var Person = new Array();
    Person[0] = 120;//定义人的体重 120斤
    Person[1] = 21;//定义人的年龄 21岁
    document.write(“体重="+Person[0]+"年龄="+Person[1]);//输出人的体重和年龄


另一个有趣的定义数组的方法:  
复制代码 代码如下:

var Person = new Array();
    Person["weight"] = 50;
    Person["age"] = 40;
    document.write("体重="+Person["weight"]+"年龄="+Person["age"]);

以上这种方法称为 “关联数组”,是通过一个特定的值来索引数组的。实际上数值数组可以被当作关联数组的一种特例来对待。

用关联数组来代替数值数组的做法意味着,我们可以通过各元素的名字而不是一个下标数字来引用它们,这样大大提高了脚本的可读性。
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