Is it safe to use the new syntactic sugar for Javascript callbacks?

Asked

Viewed 112 times

3

These days I was testing some Javascript features on Google Chrome 50 and I realized that it’s already been added to arrow function for callbacks.

Thus:

$.each([1, 2, 3], x => x * 2);

// [2, 4, 6]

In the old days I’d have to do it like this:

$.each([1, 2, 3], function (x) { return x * 2; })

We can already use this option safely for all browsers (we are in 2016!), or I still have to wait a little longer?

  • 4

    You could have native JS instead of jQuery :) The less jQuery the better :P

  • @Sergio was just an example :D

  • 1

    As @Gabrielkatakura said, we still can’t do this directly. But you can use something like Typescript (http://www.typescriptlang.org/) that "compiles" for native JS, and you can use this syntax (plus many other things that JS doesn’t have)

  • I know, it was kind of a joke. But I don’t know if join the jQuery tag if you change the examples to [1, 2, 3].map(x => x * 2);

1 answer

8


It is not safe. Ecmascript 6 is not receiving 100% support of Features yet in browsers (even modern ones), and even though the latest version of the browser supports these Eatures you will limit the operation of your website to these browsers. The ideal is to program with Ecmascript Features 5, I am adept in this idea, although there are people who use other techniques so that the code runs even with Ecmascript 3. Everything depends on your audience, usually the support for Ecmascript 5 is essential.

You can follow the support of browsers and more here: http://kangax.github.io/compat-table/es6/

And if you still want to use Ecmascript 6, look for other alternatives like Babel, Traceur, Webpack...

In this personal project of a friend I recently made a branch where I rediscovered all the code for ES6. Use Webpack with Babel in this project and it works on old browsers because the code is transposed to ES5.

https://github.com/pedrolaxe/js-terminal

Browser other questions tagged

You are not signed in. Login or sign up in order to post.