-
Notifications
You must be signed in to change notification settings - Fork 2k
Fat arrow leaks generated _this
reference to other closures
#3143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I forgot about (function(){
var $this = this;
return function ...
}.apply(this, arguments)) as I've done in michaelficarra/CoffeeScriptRedux@2eca5c2 |
I'm relatively new to these neck of the woods, but for my own enlightenment why do |
Ah crap, you're right. I'm so used to having to watch out for |
It's really a shame that we have to add an extra closure wrapper (like we used to), for this ... but there's no other possible way around it, is there? |
Let me know if that looks alright to y'all, or if it can be simplified... |
👍 Thanks for getting to this! Looks good from my (very basic) understanding of the coffee source. |
Using a fat arrow generates a
_this
variable for use in the current closure, but due to how the JS engines work it gets leaked to all closures in the function scope:(Thanks to @epidemian for the example, see end of #3052)
In the example above,
printFoo
uses a fat arrow to create a closure on_this
, butbar
accidentally gets a reference through the Closure Scope as well. Even if all references toprintFoo
are discarded, a reference tobar
will indirectly hold a reference to_this
and prevent it from being garbage collected.Given that the use of a closure is an implementation detail and not part of the fat-arrow abstraction, this seems like an undesirable side-effect. As I suggested in #3052, an IIFE approach should provide the same abstraction without this issue:
(Both @epidemian and @michaelficarra seem to somewhat agree.)
The text was updated successfully, but these errors were encountered: