5 ms·
var keydown_action_map = { 37: 'pan_left', 38: 'pan_up', 39: 'pan_right', 40: 'pan_down', 189: 'zoom_out', 187: 'zoom_in'
by reaktivo 15y ago
var keydown_action_map = {
37: 'pan_left',
38: 'pan_up',
39: 'pan_right',
40: 'pan_down',
189: 'zoom_out',
187: 'zoom_in'
}
document.addEvent('keydown', function(event) {
var action = keydown_action_map[event.keyCode];
game_pieces.each(game_piece[action], game_piece);
});
- Klonoar 15y agoI came here to post just this. While I like CS, JS isn't this horrid beast people keep making it out to be; often the solutions can be incredibly simple, it's just a matter of thinking about it.
- rimantas 15y agoThis can be written like this too: keydown_action_map = 37: 'pan_left' 38: 'pan_up' 39: 'pan_right' 40: 'pan_down' 189: 'zoom_out' 187: 'zoom_in' document.addEvent 'keydown', (event) -> action = keydown_action_map[event.keyCode] game_pieces.each game_piece[action], game_piece If you can write terse code in JS you can write it even more terse way in CS.
- masklinn 15y agoWhere "more terse" means "removing a useful keyword and replacing parens by spaces". Not exactly impressive (and coffeescript's symbolless maps read horribly badly, imo).
- rimantas 15y agoWhich keyword is useful? Var? CS will put it back at compile time, don't worry. And it will do it in more consistent manner than many coders will manually. I have no problems with symbolless maps, and CS will save your ass from being bitten by IE because of stray comma.
- thezilch 15y agoI protect my ass with VIM -- any "IDE" likely has an equivalent. au BufWrite *.js mark` | silent!%s/,\(\_s*[}\]]\)/\1/g | norm``
- masklinn 15y ago> Which keyword is useful? Var? Yes. > CS will put it back at compile time, don't worry. May I ask why you're assuming I'm a moron, exactly? > And it will do it in more consistent manner than many coders will manually. I don't know who your coders are, but you should fire them. > CS will save your ass from being bitten by IE because of stray comma. Yep, confirmed, and you should probably fire yourself as well.
- deleted 15y ago[deleted]