Re:Replace CoffeeScript with ES6
by peter_l_downs on Thursday January 29, 2015 @10:19PM
(#8970305)
It can also be nice when you're writing instance methods on a prototype. for instance:
Obviously that's a pretty contrived example but it illustrates the idea. It makes it really clear exactly what "this" means at different points in your code. As a bonus, if I had to reference the current context inside of a prototype method, "this" is now available.
[ Parent
| on HN ]
I don't like `var self` at all and I find there are vanishingly few places where you can't just bind a function appropriately, but one important thing to note, if you do go down this route, is that `self` is an alias for the `window` object, so if you happen to miss out that `var self` assignment at some point, you may get unexpected results.
On that basis, if you must do this, I would suggest `var _this`, `var scope` (though that would probably be confusing if you had the misfortune to be using Angular), `var me` (unless you happen to find anthropomorphism distasteful) or... well, anything other than `var self`, really.
Re:Replace CoffeeScript with ES6
by peter_l_downs on Sunday February 01, 2015 @01:08PM
(#8980941)
Interesting, I never knew that `self === window` – thanks for bringing that up. I'm not particularly attached to the name, just wanted to show an example where assigning `this` to some local variable is useful.