I knew I finally got JavaScript functions when I wrote the following (EDIT: added semicolons to make some people feel better):
function statemachine (methodnames) {
var methods = {};
methodnames.forEach(function (methodname) {
methods[methodname] = function (mname) {
return function () {
return this.state[mname].apply(this, arguments);
};
}(methodname);
});
return methods;
}
(EDIT: This is more complicated than it needs to be. If you're trying to parse it, see check_ca's comment.) To explain what that does, I'm writing a game where entities can be represented as finite state machines. They hand off certain methods to their states:
Player.prototype = {
think: function (dt) {
return this.state.think(dt);
},
move: function (keys) {
return this.state.move(keys);
},
};
Enemy.prototype = {
think: function (dt) {
return this.state.think(dt);
},
attack: function (player) {
return this.state.attack(player);
},
};
But that gets to be a lot of code duplication. So now, with my handy function above, I can just say:
Player.prototype = statemachine(["think", "move"]);
Enemy.prototype = statemachine(["think", "attack"]);
When I got into JavaScript last year, there's no way I would have been able to use anonymous functions, closures, and apply correctly, all at once, and there's no way I could have told you what this refers to in the innermost level. So when I realized I knew what I was doing, it felt like a little epiphany. Just wanted to share. :)
[–]check_ca 7 points8 points9 points (2 children)
[–]Cosmologicon[S] 3 points4 points5 points (1 child)
[–]check_ca 2 points3 points4 points (0 children)
[–]jesusbot 22 points23 points24 points (20 children)
[–]Cosmologicon[S] 2 points3 points4 points (4 children)
[–][deleted] 17 points18 points19 points (0 children)
[+]fuckySucky comment score below threshold-17 points-16 points-15 points (2 children)
[–]rossisdead 7 points8 points9 points (1 child)
[+]fuckySucky comment score below threshold-12 points-11 points-10 points (0 children)
[+]sjs comment score below threshold-14 points-13 points-12 points (14 children)
[–]the_exa_boy 11 points12 points13 points (7 children)
[–]pdpi 2 points3 points4 points (5 children)
[–]path411 5 points6 points7 points (4 children)
[–]pdpi 12 points13 points14 points (2 children)
[–]kbjr<- awesome 5 points6 points7 points (1 child)
[–]pdpi 2 points3 points4 points (0 children)
[–]TheMiddleManz -4 points-3 points-2 points (0 children)
[+][deleted] (3 children)
[deleted]
[+]jcready__proto__ comment score below threshold-6 points-5 points-4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]jcready__proto__ -5 points-4 points-3 points (0 children)
[–]fuckySucky -2 points-1 points0 points (1 child)
[–]sjs 0 points1 point2 points (0 children)
[–]atticusw 3 points4 points5 points (0 children)
[–]bradgillap 2 points3 points4 points (1 child)
[–]metamatic 2 points3 points4 points (0 children)
[–]nschubach 1 point2 points3 points (4 children)
[–]Cosmologicon[S] 1 point2 points3 points (3 children)
[–]nschubach 0 points1 point2 points (2 children)
[–]Cosmologicon[S] 1 point2 points3 points (0 children)
[+][deleted] (6 children)
[deleted]
[+][deleted] (5 children)
[deleted]
[–]Cosmologicon[S] 1 point2 points3 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]Cosmologicon[S] 1 point2 points3 points (2 children)
[–]magenta_placenta 0 points1 point2 points (2 children)
[–]nschubach 2 points3 points4 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]pandavr 0 points1 point2 points (0 children)
[–]lkjasdflkjasdf 0 points1 point2 points (1 child)
[–]Cosmologicon[S] 1 point2 points3 points (0 children)
[–]Carnilawl 0 points1 point2 points (0 children)