首篇博客(javascript的二维数组)
昨天晚上本来想做个JS版的QQ农场的,想用一个二维数组记录下每块地的位置,发现javascript没有二维数组.所有自定义了一个数组类
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>二维函数</title>
<script type="text/javascript" language="javascript">
/*
形成了一个rowLength,colLength的数组
*/
function DArray(rowLength, colLength) {
var dArray = new Array(rowLength);
//给每一列都添加一个关于colLength的数据
for (var i = 0; i < rowLength; i++) {
dArray[i] = new Array(colLength);
}
return dArray;
}
function GetArray() {
var dArray = new DArray(10, 10);
dArray[0][10] = 100;
dArray[0][1] = "我喜欢JS";
alert(dArray[0][10] + "," + dArray[0][1]);
}
</script>
</head>
<body>
<input type="button" value="获取二维" onclick="GetArray()" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>二维函数</title>
<script type="text/javascript" language="javascript">
/*
形成了一个rowLength,colLength的数组
*/
function DArray(rowLength, colLength) {
var dArray = new Array(rowLength);
//给每一列都添加一个关于colLength的数据
for (var i = 0; i < rowLength; i++) {
dArray[i] = new Array(colLength);
}
return dArray;
}
function GetArray() {
var dArray = new DArray(10, 10);
dArray[0][10] = 100;
dArray[0][1] = "我喜欢JS";
alert(dArray[0][10] + "," + dArray[0][1]);
}
</script>
</head>
<body>
<input type="button" value="获取二维" onclick="GetArray()" />
</body>
</html>