JS 设计的只支持一维数组,出于何种考虑,还支持二维数组的写法?
这个特性差点把我虐成狗
最初以为是 Chrome 的bug,但是测试是 IE、FireFox都支持,结果都一直,难道是标准的一部分
var testArr = [];
testArr[0,0] = 0;
console.log(testArr, " len:", testArr.length, " 0,0:", testArr[0,0]);
//IE9:日志: 0 len:1 0,0:0
//Chrome:[0] " len:" 1 " 0,0:" 0
//Firefox:Array [ 0 ] len: 1 0,0: 0
testArr[0,1] = 1;
console.log(testArr, " len:", testArr.length, " 0,1:", testArr[0,1]);
//IE9:日志: 0,1 len:2 0,1:1
//Chrome:[0, 1] " len:" 2 " 0,1:" 1
//Firefox:Array [ 0, 1 ] len: 2 0,1: 1
testArr[0,2] = 2;
console.log(testArr, " len:", testArr.length, " 0,2:", testArr[0,2]);
//IE9:日志: 0,1,2 len:3 0,2:2
//Chrome:[0, 1, 2] " len:" 3 " 0,2:" 2
//Firefox:Array [ 0, 1, 2 ] len: 3 0,2: 2
testArr[1,1] = 11;
console.log(testArr, " len:", testArr.length, " 0,1:", testArr[0,1] , " 1,1:", testArr[1,1]);
//IE9:日志: 0,11,2 len:3 0,1:11 1,1:11
//Chrome:[0, 11, 2] " len:" 3 " 0,1:" 11 " 1,1:" 11
//Firefox:Array [ 0, 11, 2 ] len: 3 0,1: 11 1,1: 11
//结论:
testArr[100,1] == testArr[1]
//下面代码是搞笑的
var arr = [1,2], val;
//val = arr[1]写成不知道算不算黑科技
val = arr[1,0,0,8,6,"是","中","国","移","动","的","客","服","电","话",1]
你知道逗号运算符吗?
所以
arr[1,2]
相当于var i = (1, 2); arr[i]
,也就是arr[2]