r/coffeescript • • Jan 10 '15
How many people replace 'this' with '@' in their CoffeeScript?
I've seen code that literally replaces every instance of 'this' with @. To me, this look really ugly, but I realize this is just a stylistic preference. Are their any disadvantages to this other than readability (which depends on the person's style)? Personally, I only use @ in my class constructors to automatically create instance properties. Everywhere else I use 'this' because it looks more like JavaScript. When do you use one over the other?
4
Upvotes
14
u/robotsmakinglove Jan 10 '15
I always use @. Mixing and matching seems like it would be annoying.
6
u/numbermess Jan 11 '15
Since it's one of the core syntactic sugars of the language, I use @ all the time. I never even thought about using 'this' instead of it.
Just curious, with regard to preserving JavaScript-isms in CoffeeScript, how do you feel about the shorthand function arrow/fat-arrow notation?
11
u/lakmeer Jan 11 '15
When using instance variables, use '@', as in '@velocity' or '@fullName'
When referring to an execution context directly, use 'this', as in 'myFunction.bind(this)' or 'return this'
3
u/crazymykl Jan 11 '15
I prefer
@overthisin all cases because a sigil being dynamically scoped is more visually distinct than one particular identifier.2
u/[deleted] Jan 15 '15
It's quicker, it's not hidden in a sea of other text. '@' stands out more than 'this' when the rest of the file is words.
1
u/cherurg Jan 24 '15
I use "this" only if I need to return it from method.