A Custom 2D JavaScript Array - The Constructor Function with a Number of Rows and Columns
(Page 5 of 5 )
We have a constructor function that has two parameters. The first one receives the number of rows; the second one receives the number of columns. Statements in the constructor functions create the rows (this[i]) and the cells (columns). Here we have a regular array with rows of the same length. The following code illustrates this:
<html>
<head>
<script type="text/javascript">
function CustomArray(y,x)
{
//form the row string
rowStr = "{"
for (j=0;j<y;j++)
{
if (j == 0)
rowStr += (j + ":" + "'" + undefined + "'");
else
rowStr += ("," + j + ":" + "'" + undefined + "'");
}
rowStr += "}";
//form the rows
for (i=0;i<x;i++)
{
//form evaluation string
evalStr = "this[i] = " + rowStr + ";";
eval(evalStr);
}
}
myArray = new CustomArray(6,5);
myArray[2][3] = "val23";
</script>
</head>
<body>
<button type="button" onclick="alert(myArray[4][1]);">Click A</button>
<button type="button" onclick="alert(myArray[2][3]);">Click B</button>
</body>
</html>
The constructor function is CustomArray(y,x). The y parameter takes the arguments for the number of rows. The x parameter takes the argument for the number of columns (cells). The first code segment of this function forms the string for the object initializer for the rows. This is the same for all rows here. The code segment uses a for-loop for this. If there are five cells, each of them will be initially undefined.
The second code segment forms the property for each row; it joins the string of the object initializer to the "this[i] = " string in order to form the evalStr variable. The JavaScript top level eval() function is used in this segment. It takes the evalStr as argument and produces the statement that defines the property of the constructor function (object type).
The last code segment in the script forms the myArray array object of 6 rows and 5 columns. 6 and 5 are arguments of the constructor function. This code segment gives the value "val23" to the cell content of the cell at row 2, column 3.
You have two HTML buttons. When clicked, the first one displays the content of the cell at row 4, column 1. This is undefined. The second one, on the other hand, will display the value "val23," which is the content of the cell at row 2, column 3.
Let us take a break here. In the next part we shall look at the properties and methods.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |