@babel/plugin-transform-arrow-functions

    In

    Out

    1. var a = function() {};
    2. var a = function(b) {
    3. return b;
    4. };
    5. const double = [1, 2, 3].map(function(num) {
    6. return num * 2;
    7. });
    8. console.log(double); // [2,4,6]
    9. var bob = {
    10. _name: "Bob",
    11. _friends: ["Sally", "Tom"],
    12. printFriends() {
    13. this._friends.forEach(function(f) {
    14. return console.log(_this._name + " knows " + f);
    15. });
    16. },
    17. console.log(bob.printFriends());

    Without options:

    1. {
    2. "plugins": ["@babel/plugin-transform-arrow-functions"]
    3. }
    1. babel --plugins @babel/plugin-transform-arrow-functions script.js

    boolean, defaults to false.

    Example

    Using spec mode with the above example produces:

    1. var _this = this;
    2. var a = function a() {
    3. babelHelpers.newArrowCheck(this, _this);
    4. }.bind(this);
    5. var a = function a(b) {
    6. babelHelpers.newArrowCheck(this, _this);
    7. return b;
    8. }.bind(this);
    9. const double = [1, 2, 3].map(
    10. babelHelpers.newArrowCheck(this, _this);
    11. }.bind(this)
    12. );
    13. console.log(double); // [2,4,6]
    14. var bob = {
    15. _name: "Bob",
    16. _friends: ["Sally", "Tom"],
    17. printFriends() {
    18. var _this2 = this;
    19. this._friends.forEach(
    20. function(f) {
    21. babelHelpers.newArrowCheck(this, _this2);
    22. return console.log(this._name + " knows " + f);
    23. }.bind(this)
    24. );
    25. },
    26. };
    27. console.log(bob.printFriends());
    • Wrap the generated function in .bind(this) and keeps uses of this inside the function as-is, instead of using a renamed .

    • Add names to arrow functions.