💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# [Arrow functions](https://babeljs.cn/docs/plugins/transform-es2015-arrow-functions) 就是我们平常说的箭头函数,可以使用 `babel-plugin-ransform-es2015-arrow-functions` 进行语法转换。 ``` npm install --save-dev babel-plugin-transform-es2015-arrow-functions ``` ## .babelrc 配置 ```json { "plugins": ["transform-es2015-arrow-functions"] } ``` ## 使用 ### 空箭头函数 in ```js var a = () => {}; ``` out ```js var a = function a() {}; ``` ### 返回值 in ```js var a = (b) => b; ``` out ```js var a = function a(b) { return b; }; ``` ### 循环中使用 in ```js const double = [1,2,3].map((num) => num * 2); ``` out ```js var double = [1, 2, 3].map(function (num) { return num * 2; }); ``` ### 构造函数中 普通的构造函数长这样: ```js function A() { this.a = 'a'; this.fun = function () { setTimeout(function () { console.log(this.a); }, 1000); }; } var a = new A(); a.fun(); // undefined ``` 使用箭头函数后: in ```js function A() { this.a = 'a' this.fun = function () { setTimeout(() => { console.log(this.a); }, 1000) } } let a = new A() a.fun() // a ``` out ```js function A() { this.a = 'a'; this.fun = function () { var _this = this; setTimeout(function () { console.log(_this.a); }, 1000); }; } var a = new A(); a.fun(); // a ```