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
Anonymous function reference #2671
Comments
→ #758 |
@satyr: That's pretty ancient. Maybe it's worth reconsidering? |
It could be optimized so that the function is only named and put in its own invoked closure if the function keyword is used, etc. The reason that @jashkenas closed #758 (in my opinion) is the weirdness with naming functions in IE. That issue also allowed for the developer to force a function to be named for no reason. This doesn't allow that. It only allows for functions to be named if the function keyword is used inside the function (which I admit is close, but slightly different). Also on a slightly more controversial note. Even jQuery is abandoning IE support soon. We should at least have a compiler switch to do so (in redux). This switch could even enable or disable certain features, such as this one. |
It's a duplicate anyway. Reopen #758 if you dare BDFL. Personally I too wanted an
More like he didn't feel the need for the proposal, or like it at all:
|
Maybe labels for closures à la coco ? @satyr what does bdfl stands for ? |
Okay so I got that right but without context :p. |
I'm not sure what problem this is supposed to solve. In cases where the anonymous function does not have a variable to reference it or is too nested inside an object so safely reference it: very.nested.obj =
countdown: (n) ->
# I want to call `countdown` here, but i don't want to call it
# very.nested.obj.countdown! We can now either assign that function to a meaningful variable: very.nested.obj =
countdown: countdown = (n) ->
if n is 0 then 'zero was reached' else countdown n - 1 Or, if we don't want the variable name to leak outside its scope, we can use a very.nested.obj =
countdown: do ->
countdown = (n) ->
if n is 0 then 'zero was reached' else countdown n - 1 Yes, kind of a noisy definition there, i personally would just use a normal variable. In any case, i think it's preferable to be explicit and name the function if we want to call it recursively than adding a new language construct just for these (arguably strange) cases. |
Sorry to be dense, but I don't understand why you'd prefer to write:
Instead of:
... in this case. Much clearer without the special single-serving magic keyword. |
For micro-DRY I guess, the same reason we prefer |
@satyr: And of course when there's no direct reference, as in @epidemian's example above. Using the long chain of member accesses, as you know, has different semantics than directly referencing a function by name. The IIFE solution is elegant but verbose. |
From #2665
I vote for a way to name or call the current function so we can have full anonymous function recursion.
Something like a re-purposing of the function keyword to mean the name of the current function:
The text was updated successfully, but these errors were encountered: