4 ms·
My most common annoyance about Javascript is the lack of bound methods like in Python Ruby etc. When you specify callbacks, you always have to provide the value
by wladimir 16y ago
My most common annoyance about Javascript is the lack of bound methods like in Python Ruby etc. When you specify callbacks, you always have to provide the value of 'this' as well. If you forget, it's basically random and you're in for some heavy debugging.
- Cushman 16y agoYou should also try CoffeeScript. dosomethingasync => #etc will automagically bind the closure to the current value of this.
- demallien 16y ago'this' is evil in Javascript. The very first line in any constructor function that I write is: var that = this; From then on, I use 'that' instead of 'this', relying on closure magic to make things work. You never have to worry about how the function is bound thereafter.
- wladimir 16y agoThanks. I think I like that solution, as it's extremely simple. Are there any drawbacks? CoffeeScript is cool, as it is basically "Python in JS" but I have some resistance against languages that convert to other languages. I'm not so sure why.
- demallien 16y agoNo, no drawbacks that I'm aware of. You're basically using a closure to bind the object to the callback, instead of using Javascript's weird 'this' binding rules, that's all...