var a = ["aa","bb","cc"];
document.write(a.unshift(11)); // -> 4 注:IE下返回undefined
document.write(a); // -> 11,aa,bb,cc
document.write(a.unshift([11,22])); // -> 5
document.write(a); // -> 11,22,11,aa,bb,cc
document.write(a.unshift("cat")); // -> 6
document.write(a); // -> cat,11,22,11,aa,bb,cc注意该方法,在IE下将返回undefined,貌似微软的bug,我在firefox下则能正确发挥数组新长度
slice
返回数组片段
var a = ['a','b','c','d','e','f','g'];
alert(a.slice(1,2)); // -> b
alert(a.slice(2)); // -> c,d,e,f,g
alert(a.slice(-4)); // -> d,e,f,g
alert(a.slice(-2,-6)); // -> 空a.slice(1,2),从下标为1开始,到下标为2之间的数,注意并不包括下标为2的元素
如果只有一个参数,则默认到数组最后
-4是表示倒数第4个元素,所以返回倒数的四个元素
最后一行,从倒数第2开始,因为是往后截取,所以显然取不到前面的元素,所以返回空数组,如果改成 a.slice(-6,-2) 则返回b,c,d,e
splice
从数组删除某片段的元素,并返回删除的元素
var a = [1,2,3,4,5,6,7,8,9];
document.write(a.splice(3,2)); // -> 4,5
document.write(a); // -> 1,2,3,6,7,8,9
document.write(a.splice(4)); // -> 7,8,9 注:IE下返回空
document.write(a); // -> 1,2,3,6
document.write(a.splice(0,1)); // -> 1
document.write(a); // -> 2,3,6
document.write(a.splice(1,1,["aa","bb","cc"])); // -> 3
document.write(a); // -> 2,aa,bb,cc,6,7,8,9
document.write(a.splice(1,2,"ee").join("#")); // -> aa,bb,cc#6
document.write(a); // -> 2,ee,7,8,9
document.write(a.splice(1,2,"cc","aa","tt").join("#")); // -> ee#7
document.write(a); // -> 2,cc,aa,tt,8,9注意该方法在IE下,第二个参数是必须的,不填则默认为0,例如a.splice(4),在IE下则返回空,效果等同于a.splice(4,0)
toString
把数组转为字符串,不只数组,所有对象均可使用该方法
var a = [5,6,7,8,9,["A","BB"],100];
document.write(a.toString()); // -> 5,6,7,8,9,A,BB,100
var b = new Date()
document.write(b.toString()); // -> Sat Aug 8 17:08:32 UTC+0800 2009
var c = function(s){
alert(s);
}
document.write(c.toString()); // -> function(s){ alert(s); }布尔值则返回true或false,对象则返回[object objectname]相比join()方法,join()只对一维数组进行替换,而toString()则把整个数组(不管一维还是多维)完全平面化
同时该方法可用于10进制、2进制、8进制、16进制转换,例如










