Created
March 1, 2011 18:05
-
-
Save CootCraig/849562 to your computer and use it in GitHub Desktop.
JS prototypal object creation 3/1/2011
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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