Skip to content

Instantly share code, notes, and snippets.

@huckfinnaafb
Created January 13, 2012 18:14
Show Gist options
  • Save huckfinnaafb/1607858 to your computer and use it in GitHub Desktop.
Save huckfinnaafb/1607858 to your computer and use it in GitHub Desktop.
function Graph(width, length) {
this.width = width;
this.length = length;
this.elements = 2; // I want to keep this
this.nodes = new Array(width * length);
var i;
for (i = 0; i < this.nodes.length; i += 1) {
this.nodes[i] = new Node(this, i);
}
}
Graph.prototype.save = function () {
return JSON.stringify(this, replacer, 4);
};
function replacer(key, value) {
var excludes = ["graph", "elements"];
if (excludes.indexOf(key) >= 0) {
return undefined;
}
return value;
}
function Node(graph, index) {
this.graph = graph; // Exclude from JSON
this.index = index;
this.elements = []; // Exclude from JSON
}
var world = new Graph(9, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment