var a = new Array(10);
var a = new Array();
a[0] = 10;
a[1] = "aaa";
a[2] = 12.6;
var a = new Array();
a[0] = true;
a[1] = document.getElementById("text");
a[2] = {x:11, y:22};
a[3] = new Array();
var a = new Array(1, 2, 3, 4, 5);
var b = [1, 2, 3, 4, 5];
var a = new Array();
a[0] = new Array();
a[0][0] = 1;
alert(a[0][0]); //弹出 1[code]
声明的时候赋值
[code]var a = new Array([1,2,3], [4,5,6], [7,8,9]);
var b = [[1,2,3], [4,5,6], [7,8,9]];
var aa = new Object();
aa.x = "cat";
aa.y = "sunny";
alert(aa.x); //弹出cat
var a = {x:"cat", y:"sunny"};
alert(a["y"]); //弹出sunny
var b = new Array();
b[2.2] = "XXXXX";
alert(b[2.2]); //-> XXXXX
var a = [1,2,3,4,5,6];
for(var i =0; i<a.length; i++){
alert(a[i]);
}
var a = [1,2,3,4,5,6];
for(var e in a){
alert(e);
}
var a = {x:1,y:2,z:3};
for(var e in a){
alert(e + ":" + a[e]);
}
var a = [123];
var b = "sunnycat";
var c = ["www",21,"ido"];
var d = {x:3.14, y:"SK"};
var e = [1,2,3,4,[5,6,[7,8]]];
alert(a.concat(b)); // -> 123,sunnycat
alert(a); // -> 123
alert(b.concat(c, d)); // -> sunnycatwww,21,ido[object Object]
alert(c.concat(b)); // -> www,21,ido,sunnycat
alert(e.concat(11,22,33).join(" # ")); // -> 1 # 2 # 3 # 4 # 5,6,7,8 # 11 # 22 # 33
var a = ['a','b','c','d','e','f','g'];
lert(a.join(",")); // -> a,b,c,d,e,f,g 相当于a.toString()
alert(a.join(" x ")); // -> a x b x c x d x e x f x g
var a = ['a','b','c','d','e','f','g',[11,22,33]];
alert(a.join(" * ")); // -> a * b * c * d * e * f * g * 11,22,33
var a = ["aa","bb","cc"];
document.write(a.pop()); // -> cc
document.write(a); // -> aa, bb
var a = ["aa","bb","cc"];
document.write(a.push("dd")); // -> 4
document.write(a); // -> aa,bb,cc,dd
document.write(a.push([1,2,3])); // -> 5
document.write(a); // -> aa,bb,cc,dd,1,2,3
var a = [11,2,3,33445,5654,654,"asd","b"];
alert(a.sort()); // -> 11,2,3,33445,5654,654,asd,b
var a = [11,2,3,33445,5654,654];
a.sort(function(a,b) {
return a - b;
});
alert(a); // -> 2,3,11,654,5654,33445
var a = [11,3,5,66,4];
alert(a.reverse()); // -> 4,66,5,3,11
var a = ['a','b','c','d','e','f','g',[4,11,33]];
alert(a.reverse()); // -> 4,11,33,g,f,e,d,c,b,a
alert(a.join(" * ")); // -> 4,11,33 * g * f * e * d * c * b * a
var a = ["aa","bb","cc"];
document.write(a.shift()); // -> aa
document.write(a); // -> bb,cc
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
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)); // -> 空
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
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); }
var a = [5,6,7,8,9,"A","BB",100];
for(var i=0; i<a.length; i++){
document.write(a[i].toString() + " 的二进制是 " + a[i].toString(2) + " ,八进制是 " + a[i].toString(8) + " ,十六进制是 " + a[i].toString(16)); // -> 4,5
}
5 的二进制是 101 ,八进制是 5 ,十六进制是 5
6 的二进制是 110 ,八进制是 6 ,十六进制是 6
7 的二进制是 111 ,八进制是 7 ,十六进制是 7
8 的二进制是 1000 ,八进制是 10 ,十六进制是 8
9 的二进制是 1001 ,八进制是 11 ,十六进制是 9
A 的二进制是 A ,八进制是 A ,十六进制是 A
BB 的二进制是 BB ,八进制是 BB ,十六进制是 BB
100 的二进制是 1100100 ,八进制是 144 ,十六进制是 64
var a = new Date();
document.write(a.toString()); // -> Sat Aug 8 17:28:36 UTC+0800 2009
document.write(a.toLocaleString()); // -> 2009年8月8日 17:28:36
document.write(a.toLocaleDateString()); // -> 2009年8月8日
var a = [1,2,3,[4,5,6,[7,8,9]]];
var b = new Date();
var c = true;
var d = function(){
alert("sunnycat");
};
document.write(a.valueOf()); // -> 1,2,3,4,5,6,7,8,9
document.write(typeof (a.valueOf())); // -> object
document.write(b.valueOf()); // -> 1249874470052
document.write(typeof(b.valueOf())); // -> number
document.write(c.valueOf()); // -> true
document.write(typeof(c.valueOf())); // -> boolean
document.write(d.valueOf()); // -> function () { alert("sunnycat"); }
document.write(typeof(d.valueOf())); // -> function
var a = [1,2,3,[4,5,6,[7,8,9]]];
var aa = a.valueOf();
document.write(aa[3][3][1]); // -> 8