Вопрос

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?

Это было полезно?

Решение 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; }.

Другие советы

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

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top