設(shè)計(jì)數(shù)組的函數(shù)方法
toString, toLocaleString, valueOf,
concat, splice, slice
indexOf,lastIndexOf,
push, pop, shift, unshift,
sort, reverse
map, reduce, reduceRight, filter, every, some, forEach
創(chuàng)建數(shù)組
數(shù)組字面量創(chuàng)建:
var arr = [val1, val2, val3];
數(shù)組構(gòu)造函數(shù):
var arr = new Array(); var arr = new Array(1, 2, 3, 5);//返回[1, 2, 3, 4, 5] var arr = new Array(2);// 返回 [ , , ] 一個(gè)參數(shù)時(shí)是數(shù)組的長度length
清空數(shù)組
清空數(shù)組有兩種方法
arr.length = 0;
arr = [];
兩者區(qū)別:js 中數(shù)組是對(duì)象,所以arr是一個(gè)指向數(shù)組值的鏈接,arr.length = 0時(shí),把數(shù)組所有值清除,執(zhí)行速度慢;arr = []時(shí)把a(bǔ)rr的鏈接指向一個(gè)新的空數(shù)組,原數(shù)組值存在于內(nèi)存中如果未被其它變量引用時(shí)則被回收。
var arr1 = [1, 2, 3];var arr2 = arr1; arr2 = [];consol