javascript面向对象技术基础(2)(转载)

javascript面向对象技术基础(二)(转载)原文:javascript面向对象技术基础(二)数组我们已经提到过,对象是无

javascript面向对象技术基础(二)(转载)
原文:javascript面向对象技术基础(二)

数组
我们已经提到过,对象是无序数据的集合,而数组则是有序数据的集合,数组中的数据(元素)通过索引(从0开始)来访问,数组中的数据可以是任何的数据类型.数组本身仍旧是对象,但是由于数组的很多特性,通常情况下把数组和对象区别开来分别对待(Throughout this book, objects and arrays are often treated as distinct datatypes.This is a useful and reasonable simplification; you can treat objects and arrays as separate types for most of your JavaScript programming.To fully understand the behavior of objects and arrays, however, you have to know the truth: an array is nothing more than an object with a thin layer of extra functionality. You can see this with the typeof operator: applied to an array value, it returns
the string "object".? --section7.5).
创建数组可以用"[]"操作符,或者是用Array()构造函数来new一个.

?对于数组的其他方法诸如join/reverse等等,在这就不再一一举例.

通过上面的解释,我们已经知道,对象的属性值是通过属性的名字(字符串类型)来获取,而数组的元素是通过索引(整数型 0~~2**32-1)来得到值.数组本身也是一个对象,所以对象属性的操作也完全适合于数组.

Js代码
    var?array?=?new?Array("no1","no2");??array["po"]?=?"props1";??alert(array.length);???//2??//对于数组来说,array[0]同array["0"]效果是一样的(?不确定,测试时如此)??alert(array[0]?+?"_"?+?array["1"]?+?"_"?+?array.po);//no1_no2_props1?