Not binding an anonymous function in Coffeescript executes with `this` bound to `window` ... but arrow functions do not provide a non-null `this`
See original GitHub issue _.defer -> @someWindowProp?
I get this output:
_.defer => { ... }
Technically, this is not preserving behaviour since this
is now null
and this causes an exception to be thrown. In the Coffeescript runtime (i.e: using just function
then you get a case where you would end up just being run in the window
context. In most cases in our code base, it’s actually an error! (someone thought they were bound but were not and did a null check for other reasons) but I guess technically this goes against “out of the box correctness”
Not sure how to fix – I have a few ideas but not 100% there.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Passing an arrow function vs passing the function
The second case an anonymous function is created which executes the handleChange method and and thereby providing it the context .
Read more >coffee-script/index.html.erb at master - documentation
Launch an interactive CoffeeScript session to try short snippets. Identical to calling <tt>coffee</tt> with no arguments.
Read more >Top 7 Reasons To Love CoffeeScript
Function binding has a similar shortcut. When defining a function I need to make sure is scoped (or "bound") to the current "this"...
Read more >CoffeeScript
The fat arrow => can be used to both define a function, and to bind it to the current value of this, right...
Read more >IcedCoffeeScript - About Me
IcedCoffeeScript is a superset of CoffeeScript. The iced interpreter is a drop-in replacement for the standard coffee interpreter; it will interpret almost ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for taking a look. That’s what I gathered as well. As I said, in my case most of these are actually in error and someone isn’t actually looking for a window prop, rather, a class prop but forgot to bind, so there was already a logic bug. In some ways, decaffeinate did me a favour. 😉
Thanks.
oops, I may have simplied too much. I’ll try and create a REPL for you – sorry, I can’t link to proprietary code and may have “trimmed” out the source of the problem.
Lemme do that for you.