-
Notifications
You must be signed in to change notification settings - Fork 2k
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
The function keyword #758
Comments
You shouldn't have to worry about the security of a local variable. Is there a situation where this would work, and referencing a local variable wouldn't? |
I was thinking more about when it's just annoying to have to make that assignment. For example (quite contrived but also very plausible):
If we export
but it's annoying and leaves you with this weird local variable floating around just so you can safely refer to the function. |
In my opinion, it's nicer to refer to a meaningfully-named local variable than it is to a generic "function". If this is more about named functions than recursive forms, we can certainly find a way to compile named functions for IE -- it just takes a little more doing. For example, functions assigned as object properties can safely be named, because the variables don't clash. Functions with names that do clash can be assigned with slightly altered names. Functions that are assigned to local variables can be declared with that name, instead of assigned... |
Really? As I understand it (I don't have anything to test it on), IE creates the named function as a definition, meaning it eats every variable in the same scope regardless of where it's actually defined.
In order to make it work properly for everything, we'd need to apply the fix to everything that isn't a declaration. And I personally feel a lot better about being able to safely refer to the function without the use of a variable. Plus if I rename variables on a whim, which I tend to do fairly often, there's less refactoring to do. |
Under this proposal, would it be possible to compile |
|
Related issues: * jashkenas/coffeescript#15 * jashkenas/coffeescript#366 * jashkenas/coffeescript#758 an many more. See also https://github.com/jashkenas/coffee-script/wiki/FAQ Any objections before I merge this into `master`?
Now that we have a format for allowing named functions without breaking IE, it would be great for all functions to have this format. The format involves defining and calling another function to create a closure though, so it's not really realistic to use this for every function.
As such, I would like to propose introducing the
function
keyword, which is obviously already excluded as a JS keyword from CS. Use of the keyword names the function directly above it with the name of the variable it is assigned to (anon
if there isn't one), and then compiles any use offunction
inside of it to the name of that function.becomes:
It's a great way to refer to the function above if you don't need to assign it to a variable or if you're not sure about the security of said variable. It can also just be used to name a function if you want to use it for reflection.
I've already done most of the work towards implementing its use, and I'm curious to see what you think of it.
The text was updated successfully, but these errors were encountered: