javascript 箭头函数(lambda表达式) 作者:马育民 • 2021-01-18 17:28 • 阅读:10040 # 介绍 箭头函数,Arrow Function,是 **ES6标准** 新增的函数,相当于 **匿名函数** 的简写形式 # 匿名函数 VS 箭头函数 [](https://www.malaoshi.top/upload/pic/js/QQ20210118223444.png) - **箭头函数** 与 匿名函数 写法 **几乎相同**,只是 **少了** `function` ,**多了** `=>` 根据 **形参的数量**,函数中 **语句的数量**,箭头函数提供了多种 **简写形式** - 箭头函数中的 `this` 指向与 匿名函数不同 # 多个参数,多条语句 即:上图的代码 `()`、`{}` 不能省略 ``` var add = function (a,b) { console.log("a="+a) console.log("b="+b) return a+b } var add2 = (a,b) => { console.log("a="+a) console.log("b="+b) return a+b } var res=add(1,2) console.log("res="+res) var res=add2(1,2) console.log("res="+res) ``` # 只有一个形参,可省略 () ``` var test = function (a) { return a*2 } var test2 = a => { return a*2 } var res=test(3) console.log("res="+res) var res=test2(3) console.log("res="+res) ``` # {} 只有一条语句,可省略 {} ``` var test = function (a) { return a*2 } // 必须不能写 return,自动将该行代码的结果返回 var test2 = a => a*2 var res=test(3) console.log("res="+res) var res=test2(3) console.log("res="+res) ``` # 没有形参,一条语句 `()`不可省略、`{}` 可以省略 ``` var test = function () { return 10 } var test2 = () => 10 var res=test() console.log("res="+res) var res=test2() console.log("res="+res) ``` 原文出处:http://malaoshi.top/show_1IXQlsFcERf.html