How to use Private Methods in JavaScript
andrewkelley.me
andrewkelley.me
Interestingly, just defining initializeSomethingElse within Cell only results in ~10% slowdown, and that yields a truly private method. E.g.
function Cell(x, y) {
var self = this;
self.x = x;
self.y = y;
self.things = [1, 2, 3];
// private -unreachable- method
initializeSomethingElse();
// Private methods here
function initializeSomethingElse() {
self.dir = Math.atan2(self.y, self.x);
self.total = 0;
self.things.forEach(function(thing) {
self.total += thing;
});
}
}Unfortunately, it was much slower when I tested it last night. Close to 60% slower compared to your original solution, or 7% - 10% slower than the original prototype method version.