Tianyang Li
Tianyang Li

Reputation: 1785

What does `=>` do in Mozilla's JavaScript?

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?

Upvotes: 3

Views: 95

Answers (3)

BrunoLM
BrunoLM

Reputation: 100381

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.

Upvotes: 1

user1046334
user1046334

Reputation:

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; }.

Upvotes: 2

Bergi
Bergi

Reputation: 665276

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") :-)

Upvotes: 3

Related Questions