4 ms·
You can further improve dispatch tables in js by using bind. so something like: north: function() { movePlayer("north"); }, becomes: north: movePlayer.b
by SoapSeller 13y ago
You can further improve dispatch tables in js by using bind.
so something like:
north: function() { movePlayer("north"); },
becomes:
north: movePlayer.bind(this, "north"),
or using underscore.js(for greater browser compatibility):
north: _.bind(movePlayer, this, "north"),
- taf2 13y agoor instead of a whole library use mozilla's compatibility when using bind... see the function here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
- k3n 13y agoBecause modifying object prototypes is the way to go?
- taf2 13y agoDepends if you're in an environment you control then yes - when embedding your scripts, don't do it. Create your own isolated functions and assume anything and everything can be broken e.g. Array.prototype is often modified with broken or semi incomplete functions like broken versions of indexOf
- jstanley 13y agoHow is this an improvement? It looks basically equivalent but more confusing to me.
- xhrpost 13y agoIt's just a way of getting rid of the redundant "function()" syntax. Unfortunately with bind(), we have to set the scope even though it isn't always necessary. So in this case, the first parameter "this" is also probably redundant. The old PrototypeJS curry() would have been the least redundant solution: movePlayer.curry("north")
- k3n 13y agoThere's also partial(): 1. http://documentcloud.github.io/underscore/#partial http://documentcloud.github.io/underscore/#partial
- jeffamcgee 13y agoIf you use es5-shim (https://github.com/kriskowal/es5-shim https://github.com/kriskowal/es5-shim), you can say movePlayer.bind(this, "north") on older browsers.