I do not know if I can be more clear than the article you are already reading, but I will try.
This: new Array(25) equivalent to this: [undefined, undefined, undefined, ...25x]
In your code, you have the following:
var grid = ["top left", "top middle", "top right", "bottom left", "bottom middle", "bottom right"];
then declaring the same var again:
var grid = new Grid(5, 5);
So, in the end, grid is [undefined, undefined, undefined, ...] . This is why you get undefined before setting anything up.
get and set , just find the position of the element in the array and read or write the value at the specified position. This is the code that finds the position in the array: vector.x+this.width*vector.y . Let me break it:
vector.x = table column
vector.y = table row
Imagine a table 3x2 = ['row0 col0', 'row0 col1', 'row0 col2', 'row1 col0', 'row1 col1', 'row1 col2']
now we need an element in the column col 2, row 1, so new Vector(2, 1) . This element is at position 5 in our array. So, starting from row 1 (this.width * vector.y) = (3 * 1), get the element in column 2 (+ vector.x) = (+ 2)
this.width is the size of each line, so when multiplied by vector.y this means that vector.y is equivalent to a certain number of lines. Then from there you just summarize the position of the column (vector.x).
Presentation of tabular data
A table has several rows, and each row has several columns, so you can represent the table using an array for each row, for example:
row1 = ['item1', 'item2']; row2 = ['item3', 'item4']; table = [row1, row2];
This will give you a multidimensional array: [ ['item1', 'item2'], ['item3', 'item4'] ]
The value for accessing data of the type: table[ rowIndex ][ columnIndex ]
But the method you use saves all the items in one list, one array: table = ['item1', 'item2', 'item3', 'item4']
Now let's say that we want to find the same element as in the previous example using rowIndex and columnIndex , except that this time there is only one list of elements, so we need to combine rowIndex and columnIndex into one number to get the element using: table[ indexOfItemIWant ] . How do we do this? You know that all lines are listed one after another, and all lines have the same number of elements on it. So, to find the beginning of a line in our list, we multiply the size of the lines by the number of lines that we want to skip. In the table, where each row has two elements, as in our example, the first row starts at position 0 and occupies two positions, so the next row starts at position 0 + 2, then the next one, at position 0 +2 + 2, then 0 + 2 +2 +2, etc., Why do you use width (the number of elements per line) times the line I want to get (vector.y).