7 ms·
This "pattern" is wrong, it makes debugging much harder. In some cases less code means more pain and this is such case.
by ghostdiver 13y ago
This "pattern" is wrong, it makes debugging much harder. In some cases less code means more pain and this is such case.
- Vekz 13y agoThis is poor communication. Saying its 'wrong' and makes it 'harder' are your personal generalizations. Thats an empty statement. How does it do either of those? This pattern encourages naming of anonymous functions that are used in callbacks. Which creates simpler stack traces. The new piece here about passing additional params to bind is also helpful, in that it will allow you to carry params across a callback stack. For me this will make carrying an express apps request and response objects across a callback chain much easier.
- ghostdiver 13y agoIt forces programmer to make few more steps during debugging process, using conditional breakpoints in particular. not a big deal to put conditional breakpoint in handleStreamEvent whereever it is, but in long run it just a waste of time. this.setup = function () { this.on('tweet', this.handleStreamEvent.bind(this, 'tweet')); this.on('retweet', this.handleStreamEvent.bind(this, 'retweet')); }; This pattern will be obsolote as soon as arrow function gets implemented by all major browsers: this.setup = function () { this.on('tweet', () => this.handleStreamEvent('tweet')); this.on('retweet', () => this.handleStreamEvent('retweet')); };
- deleted 13y ago[deleted]
- badman_ting 13y agoAh, but what is the value of 'this' when those handler functions are called?
- epidemian 13y agoArrow functions preserve the 'this' of their enclosing context: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/arrow_functions#Lexical_this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... :)
- ch0wn 13y agoThose are not equivalent. You still have to pass on the parameters: this.setup = function () { this.on('tweet', (e, data) => this.handleStreamEvent('tweet', e, data)); this.on('retweet', (e, data) => this.handleStreamEvent('retweet', e, data)); };
- ianstormtaylor 13y agoGood catch. Once they are added though, it feels a lot more readable than the "bind" alternative.