Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by taf2 on Monday May 13, 2013 @10:01AM
(#5698663)
or instead of a whole library use mozilla's compatibility when using bind... see the function here: https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by k3n on Monday May 13, 2013 @12:50PM
(#5699755)
Because modifying object prototypes is the way to go?
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by taf2 on Tuesday May 14, 2013 @03:11AM
(#5703394)
Depends 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
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by jstanley on Monday May 13, 2013 @11:51AM
(#5699354)
How is this an improvement?
It looks basically equivalent but more confusing to me.
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by xhrpost on Monday May 13, 2013 @12:06PM
(#5699461)
It'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")
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by k3n on Monday May 13, 2013 @12:52PM
(#5699770)
There's also partial():
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by malandrew on Monday May 13, 2013 @02:24PM
(#5700454)
It would be nice if they had called it curry() or at least created a curry() alias.
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by xhrpost on Monday May 13, 2013 @01:14PM
(#5699925)
Nice, hadn't seen that before, looks like it was just added early this year.
Re:Using Dispatch Tables to Avoid Conditionals in JavaScript
by jeffamcgee on Monday May 13, 2013 @11:54AM
(#5699374)
If you use es5-shim (https://github.com/kriskowal/es5-shim), you can say movePlayer.bind(this, "north") on older browsers.
so something like:
becomes: or using underscore.js(for greater browser compatibility):