<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//原本的函数
function demo_1(a,b){
return a+b;
}
console.log(demo_1(1,2));
//简化为箭头函数
demo_2=(a,b)=>{
return a+b;
}
console.log(demo_2(1,2));
//如果箭头函数只有一句返回值,还可以简写为
demo_3=(a,b)=>a+b;
console.log(demo_3(1,2));
//利用箭头函数实现map和reduce
let arr = [1,2,3,4];
var map = arr.map((currentValue)=>{
return currentValue*2;
});
var reduce = arr.reduce((accumulator, currentValue) => {
return accumulator+currentValue;
});
console.log("map函数后得到的数组");
console.log(map);
console.log("reduce函数后得到的数组");
console.log(reduce);
</script>
</body>
</html>
4_箭头函数
最新推荐文章于 2022-04-28 17:29:44 发布