当前位置:网站首页>箭头函数的使用
箭头函数的使用
2022-07-15 16:51:00 【至尊绝伦】
箭头函数是ES6之后增加的一种编写函数的方法,并且它比函数表达式要更加简洁。
- 箭头函数不会绑定this、arguments属性;
- 箭头函数不能作为构造函数来使用
箭头函数的编写形式
- ()函数的参数
- {}函数的执行体
nums.forEach((item, index, arr)=>{
})箭头函数的编写优化
优化一:如果只有一个参数()可以省略
nums.forEach(item => {})优化二:如果函数执行体中只有一行代码,那么可以省略大括号
并且这行代码的放回值会作为整个函数的返回值
nums.forEach(item => console.log(item))
nums.filter(item => true)优化三:如果函数执行体只有返回一个对象,那么需要给这个对象加上()
var foo = () => {
return { name: "abc" }
}
var bar = () => ({name: "abc"})案例:使用箭头函数将一个数组里面的偶数的平方和输出
var nums = [20, 40, 11, 22, 45, 36, 89]
var result = nums.filter(item => item % 2 === 0)
.map(item => item ** 2)
.reduce((preValue, item)=> preValue + item)
console.log(result)边栏推荐
猜你喜欢

TP5 分页一些小点

【云原生】3.4 RuoYi-Cloud部署实战(上)

YOLACT结构图

H264-解码顺序 显示顺序 参考顺序

TimeSformer: 只靠 Transformer 就能理解视频?注意力机制的又一次进击
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (4)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (4)

Compileflow Taobao workflow engine

8. MySQL -- 触发器

C语言 第六章 函数

Serialization and deserialization of flip word /maxqueue/ quadratic tree
随机推荐
8. MySQL -- trigger
Drive free USB to serial port billboard chip (ldr2001)
C语言 栈的顺序表实现
左旋转字符串
Is there a limit on the length of string? How much is it?
翻转单词/Maxqueue/二次树的序列化和反序列化
DEBUG系统
rhcsa笔记二
Redis-安装&&启动
markdown学习笔记 第二章 基本语法 (非markdown编辑器下显示)
数组中的重复数字
示波器使用概念记录
Dix règles d'optimisation pour clickhouse SQL
Private cloud ranking of Tangmen concealed weapons
Implementation of sequence table in C language stack
C语言 第十章 指针
Asymmetric encryption RSA and symmetric encryption AES project application
Based on taskscheduler and crontask, realize the function of dynamically adding and deleting scheduled tasks
oracle怎样转义单引号
MAZAK手轮维修马扎克小巨人数控机床手柄操作器维修AV-EAHS-382-1