Skip to content

Instantly share code, notes, and snippets.

@adriancooney
adriancooney / Array2D.js
Created April 4, 2012 13:52
Javascript 2D Array
/* Extremely simple definition of a 2D array in Javascript. Javascript doesn't allow for a match-all getter so a length has to be defined. This defines the height or rows of the array but not the width; the width or columns is as big as you want it to be, sugar. For some odd reason, the __defineGetter__ doesn't send the name of the current "get" to the callback (which is silly in my opinion for situations exactly like these) so I had to convert the functions to strings. */
var Array2D = function(l) {
if(!l) throw new Error("Please specify a length for your Array2D");
this.arr = [];
this.definedLength = l;
for(var i = 0; i < l; i++) {
this.__defineGetter__(i, Function("var i = " + i + ";if(!this.arr[i]) this.arr[i] = [];return this.arr[i];"));
}