javacript没有多维数组只能模拟?
为什么说javacript没有多维数组只能模拟?但是我看到是可以实现多维数组的啊!这是为什么呢?下面我们来实验下。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <title>hexagon</title> <style type="text/css"> #AA{width:10px;} </style> <script type="text/javascript"> window.onload=function(){ var w = 100, h = 100, top = 100, left = 100, red = "red", blue = "blue", col=null, bb=null; var qq =new Array(); for(var i=0; i<10;i++){ for(var j=0; j<10;j++){ qq[i][j]=0; } }; qq[0][3]=5; for(var i = 0; i<10;i++){ top = 110*i+50; for(var j=0; j<10;j++){ bb=red; if(qq[i][j]){bb=blue;}; left = 110*j+50 document.write('<div style="position:absolute;left:'+left+'px;top:'+top+'px;width:'+w+'px;height:'+h+'px;background-color:'+bb+';"></div>')} } } </script> </script> </head> <body> </body> </html>
这样会报错
TypeError: qq[i] is undefined
qq[i][j]=0;
就是说直接使用多维数组是不行的哦,那就是说javacript不支持多维数组哦!那我们平时是怎么用的哦?
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <title>hexagon</title> <style type="text/css"> #AA{width:10px;} </style> <script type="text/javascript"> window.onload=function(){ var w = 100, h = 100, top = 100, left = 100, red = "red", blue = "blue", col=null, bb=null; var qq =new Array(); for(var i=0; i<10;i++){ qq[i] = new Array;//要在里面每个数组再设置为数组哦 for(var j=0; j<10;j++){ qq[i][j]=0; } }; qq[0][3]=5; for(var i = 0; i<10;i++){ top = 110*i+50; for(var j=0; j<10;j++){ bb=red; if(qq[i][j]){bb=blue;}; left = 110*j+50 document.write('<div style="position:absolute;left:'+left+'px;top:'+top+'px;width:'+w+'px;height:'+h+'px;background-color:'+bb+';"></div>')} } } </script> </script> </head> <body> </body> </html>
要再在每个数组里的元素设置成数组才可以做到模拟多维数组的哦!!!