Skip to content

Instantly share code, notes, and snippets.

@CootCraig
Created March 1, 2011 18:05
Show Gist options
  • Save CootCraig/849562 to your computer and use it in GitHub Desktop.
Save CootCraig/849562 to your computer and use it in GitHub Desktop.
JS prototypal object creation 3/1/2011
// Working on my JS idioms
// fragment, not tried yet.
neighborhood = new function() {
// prototype object saved in closure
var proto = {
add_cell: function(location) {
var key = location.key();
if (this.cells_and_neighbors.hasOwnProperty(key)) {
this.cells_and_neighbors[key].revive();
} else {
this.cells_and_neighbors[key] = live_cell(location);
}
},
add_neighbor: function(location) {
var key = location.key();
if (!this.cells_and_neighbors.hasOwnProperty(key)) {
this.cells_and_neighbors[key] = dead_cell(location);
}
this.cells_and_neighbors[key].increment_neighbor_count();
},
};
return function() {
// functions put in prototype
var neighborhood = coot.create(proto); // local version of object.create()
// initialize "instance" variables
neighborhood.cells_and_neighbors = {};
return neighborhood;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment