3

In Mozilla's sample code for Downloads.jsm there's a few lines that use => which I don't know the meaning of:

let view = {
  onDownloadAdded: download => console.log("Added", download),
  onDownloadChanged: download => console.log("Changed", download),
  onDownloadRemoved: download => console.log("Removed", download),
};

What does => do here?

Tianyang Li
  • 1,755
  • 5
  • 26
  • 42
  • possible duplicate of [What's the meaning of “=>” in JavaScript?](http://stackoverflow.com/q/24900875/1048572) – Bergi Dec 17 '15 at 18:12

3 Answers3

3

They're a special function literal notation, called arrow functions, introduced with ES6. It's basically the same as in coffeescript.

It could have been shorter written as console.bind(console, "Added") :-)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

It's the syntax of upcoming version of JavaSctipt, the ECMAScript 6, aka Harmony.

The x => expr syntax stands, more or less (with differences with treatment of this), for function (x) { return expr; }.

1

That is called "arrow functions" or "lambda expressions".

If you want to use that and not wait for ECMAScript 6, you can have a look at , you don't have to worry about browser compatibility and this kind of stuff.

You can check an example here.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452