21cae9d67cc6bd788e920911c4af8d452d5a5e03b277e3a66913b08d5edd9a83370bab73d7a1d309986d049ee9959bb66867f61018d5ddb732ddbbe75a2ddf 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # babel-plugin-transform-es2015-arrow-functions
  2. > Compile ES2015 arrow functions to ES5
  3. ## Example
  4. **In**
  5. ```javascript
  6. var a = () => {};
  7. var a = (b) => b;
  8. const double = [1,2,3].map((num) => num * 2);
  9. console.log(double); // [2,4,6]
  10. var bob = {
  11. _name: "Bob",
  12. _friends: ["Sally", "Tom"],
  13. printFriends() {
  14. this._friends.forEach(f =>
  15. console.log(this._name + " knows " + f));
  16. }
  17. };
  18. console.log(bob.printFriends());
  19. ```
  20. **Out**
  21. ```javascript
  22. var a = function a() {};
  23. var a = function a(b) {
  24. return b;
  25. };
  26. var double = [1, 2, 3].map(function (num) {
  27. return num * 2;
  28. });
  29. console.log(double); // [2,4,6]
  30. var bob = {
  31. _name: "Bob",
  32. _friends: ["Sally", "Tom"],
  33. printFriends: function printFriends() {
  34. var _this = this;
  35. this._friends.forEach(function (f) {
  36. return console.log(_this._name + " knows " + f);
  37. });
  38. }
  39. };
  40. console.log(bob.printFriends());
  41. ```
  42. [Try in REPL](http://babeljs.io/repl/#?evaluate=true&lineWrap=true&presets=es2015%2Ces2015-loose&experimental=false&loose=false&spec=false&code=var%20a%20%3D%20()%20%3D%3E%20%7B%7D%3B%0Avar%20a%20%3D%20(b)%20%3D%3E%20b%3B%0A%0Aconst%20double%20%3D%20%5B1%2C2%2C3%5D.map((num)%20%3D%3E%20num%20*%202)%3B%0Aconsole.log(double)%3B%20%2F%2F%20%5B2%2C4%2C6%5D%0A%0Avar%20bob%20%3D%20%7B%0A%20%20_name%3A%20%22Bob%22%2C%0A%20%20_friends%3A%20%5B%22Sally%22%2C%20%22Tom%22%5D%2C%0A%20%20printFriends()%20%7B%0A%20%20%20%20this._friends.forEach(f%20%3D%3E%0A%20%20%20%20%20%20console.log(this._name%20%2B%20%22%20knows%20%22%20%2B%20f))%3B%0A%20%20%7D%0A%7D%3B%0Aconsole.log(bob.printFriends())%3B&playground=true)
  43. ## Installation
  44. ```sh
  45. npm install --save-dev babel-plugin-transform-es2015-arrow-functions
  46. ```
  47. ## Usage
  48. ### Via `.babelrc` (Recommended)
  49. **.babelrc**
  50. ```js
  51. // without options
  52. {
  53. "plugins": ["transform-es2015-arrow-functions"]
  54. }
  55. // with options
  56. {
  57. "plugins": [
  58. ["transform-es2015-arrow-functions", { "spec": true }]
  59. ]
  60. }
  61. ```
  62. ### Via CLI
  63. ```sh
  64. babel --plugins transform-es2015-arrow-functions script.js
  65. ```
  66. ### Via Node API
  67. ```javascript
  68. require("babel-core").transform("code", {
  69. plugins: ["transform-es2015-arrow-functions"]
  70. });
  71. ```
  72. ## Options
  73. * `spec` - This option wraps the generated function in `.bind(this)` and keeps uses of `this` inside the function as-is, instead of using a renamed `this`. It also adds a runtime check to ensure the functions are not instantiated.