The proposed function syntax looks exactly like C# lambda functions. Even the (this, x) => {} format is similar to how C# extension methods work. Extension methods require "this" to be the first parameter and define what objects the method can act on.
It's really interesting to see that bleed over from a completely different type of language.
I didn't know what to make of that comment. Dynamic this-binding is used heavily in any OO javascript code; it's not a feature that should be ignored when designing the new syntax.
To me, writing 'this' as part of the parameter list for methods is more confusing than having a second arrow operator.
Looks really similar to Coffeescript. Is it safe to say that this new Javascript feature is a direct result of Coffeescript's influence? I hope so: Javascript is full of little horrors like the 'this' keyword, and it seems like it took a whole new language to convince people that it needs fixing.
I think its absolutely because of languages like coffee script that JavaScript is changing, but don't take my word for it. You can find statments from standards makers to the same effect.
This is a great talk to watch about the development of JS. It's been a while since I watched it, but the point I was making, and the point I gleaned from this talk, is that new ideas need a test bed, and according to Eich Coffeescript is one of those places where new ideas are tested, and derived from.
I don't think it matters what came first. CoffeeScript has had a great effect on hearts, and minds because you can use it. It matters that CoffeeScript exists because people can wrap there head around new concepts and ideas. Only once people really understand an idea can we determine if it's good, or bad.
Fat arrow is well understood in the JS community because of CoffeeScript, and that is one reason why people are pushing for fat arrow to be accepted. I personally think CoffeeScripts has had a very large impact on the idea.
I think it's safe to say. Arrow functions have existed in many different languages, but this particular flavor of using the fat arrow to declare a "lexical `this`" function, where the value of "this" within the function body preserves the value of "this" outside, is in CoffeeScript:
Jeremy, would you mind sharing what the inspiration was for CoffeeScript's fat arrow? Was that token inspired by another language or was it something you just came up with on your own?
Sure -- the syntax for arrow functions has been floating around for ages, in Haskell, C#, and probably most relevant for me, the "stabby lambdas" in Ruby 1.9.
One of the first ideas with CoffeeScript was to simplify function syntax, mostly because JavaScript's anonymous functions are so useful and pervasive that you end up with a lot of this:
... where the "word" function there doesn't really tell you anything meaningful about what you're trying to accomplish.
Arrows were appealing for a shorthand function syntax because of their visual representation of what happens in a (pure) function. Input goes in one side, output comes out the other. The input determines, or points to, the output. Since we use parentheses symmetrically to group parameters to a function, both when you define a function and when you call one, we arrive at this:
(input) -> output
... or with our initial example:
_.each toRemove, (key) -> delete data[key]
That was the thinking. CoffeeScript actually originally used => for all functions, but the fat arrow distinction was introduced when we introduced bound (lexical "this") functions as an alternative to normal (dynamic "this") functions.
Thanks for the detailed response. I thought it might have been inspired from Haskell or Ruby, but was surprised to find out that C# has a similar syntactic feature. So I was curious if there was an "official" etymology. Sounds like an amalgamation of all of the above.
At this point it feels like the next version of Javascript should just standardize CoffeeScript syntax and add a few much needed features CoffeeScript can't currently do because of JS limitations.
Can't wait for this to happen and gain widespread browser support so I can actually use it.
JavaScript is a beautiful language trapped in a lame syntax, peppered with poor semantic choices based on what was considered "just the way it is" back then.
There are things about coffeescript that I just don't think are good ideas, like "unless" or "if" at the end. The person reading the code will think "Ok, now I'm doing x ... oh wait I'm not". the flow of it is just backwards.
The "myFunction(item) for item in array" syntax also seems wrong way around. You use "item" before you say what it is.
I'm not an expert. These are just initial impressions, but coffeescript seems to have some good ideas, but tries to do too much. It would be interesting to see what thought process went into deciding to include certain constructs. Was it just an experiment? Are there reasons that might persuade me?
Ultimately the better JavaScript is also going to be JavaScript.
The problem is we're mainly conditioned to expect the condition up front, notwithstanding Perl's initial slide down this slippery slope. Ugly syntax seems an argument for improving the syntax, not changing the grammar.
Does a language need three ways to say "if x do y" ? or maybe there's an "unless !x do y" construct and it's 4 ways.
This to me looks like going out of your way to have lots of ways to do the same thing. I never cared for it in PERL either.
The argument that "it reads like English" is not one that I like much - The history of making programming languages read like English is a long succession of dead ends. Programming languages work more like formal maths proofs than they do like English prose. Rather make the language readable on its own terms.
> The history of making programming languages read like English is a long succession of dead ends.
There certainly have been notable failures, such as Applescript, but I find it hard to dismiss Coffeescript, Ruby, SQL and FORTRAN which have all been quite popular.
That dosen't really say much about the language itself though, which I think was what was being called 'beautiful' (as opposed the ecosystem).
Besides, couldn't the same argument be made about C? Sure I know a lot of people 'like' C, but I don't think I've ever heard of it being referred to as 'beautiful'...
Holy shit something that has been around for a while and works in all browsers now with coffeescript is now something we may be able to use with regular javascript in like 10 years! What's next, a for loop? Am i pushing it too far?
(flame prevention: this is kind of a joke. i understand that it actually is pushing towards a better language, it's just such a slow and painful process compared with just using coffeescript)
It's nice that somebody like Crockford writes an article like this, telling us how to actually use the new syntax. The original ES Wiki article is probably great for browser vendors but i didn't quite get it the first time i read it.
It sounds like people will prefer the (a,b) => {} syntax over function (a,b) {} simply because it's more concise and looks better, not because of its usefulness.
Sometimes concise is (arguably, of course) better.
Whenever I pass a simple and short function, for example as a predicate to grep/filter or as a tiny implementation for map/reduce I feel that "function" is half of what I type - and distracting. Worse, explicitly "return"ing adds more noise.
Most of my anonymous functions are really, really small. If they are usually on the level of "d % 2 == 0" or "d.slice(42)" then removing the overhead of "function (d) { return" seems a good idea. Not a huge deal, but nice nevertheless.
It's really interesting to see that bleed over from a completely different type of language.