当前位置:网站首页>ES6箭头函数
ES6箭头函数
2022-07-22 05:07:00 【weixin_47254122】
撰写时间:2022 年 7 月 16日
ES6箭头函数
什么是箭头函数:
认识箭头函数:
const add = (x,y) =>{
return x+y;
};
console.log(add(1,1));
箭头函数的结构:
const、let 函数名=(参数1,…)=>函数体
如何将一般函数改写成箭头函数:
//声明形式
function add() {}
// 声明形式->函数表达式形式
const add = function () {};
// 函数表达式形式->箭头函数
const add = () => {};
箭头函数的注意事项:
单个参数:
// 单个参数可以省略圆括号
const add = x => {
return x + 1;
};
console.log(add(1));
无参数或多个参数不能省略圆括号:
const add = () => {//无参数的时候 括号不能省略
return 1 + 1;
};
const add1 = (x, y) => {//多个参数的时候,括号不能省略
return x + y;
};
console.log(add1(1, 1));
单行函数体=>单行函数体可以同时省略 {} 和 return:
const add = (x, y) => {
return x + y;
};
//简写形式 const add=(x,y)=>x+y
console.log(add(1,4));//5
多行函数体不能再化简了:
const add = (x, y) => {
const sum = x + y;
return sum;
};
单行对象 =>如果箭头函数返回单行对象,可以在 {} 外面加上 (),让浏览器不再认为那是函数体的花括号:
const add=(x,y)=>{
return{
sum:x+y
}
}
//简写形式: 在{}外面加上(),让浏览器不再认为那是函数体的花括号
const add = (x, y) => ({
value: x + y
});
边栏推荐
- Mecol Studio - harmonyos second assignment
- TensorFlow 预测日销量
- 高数_第3章重积分
- Switch and router technology: OSPF route redistribution, OSPF NSSA area and OSPF virtual link
- pygame 电子战模拟效果
- UE4 关卡蓝图实现开关门
- 看融合数学教学的steam教育模式
- Understand recursion and iteration
- PHP QR code decoding qrreader class | QR code image to string
- UE4 用灰度图构建地形
猜你喜欢
随机推荐
接手RN项目遇到的问题及解决 自用
lvs看这篇就够了
Kingbasees Security Guide for Jincang database -- 2.2. prevention of kingbasees' threats to database security
C language program practice - (write a function, its prototype is int continumax (char *outputstr, char *intputstr))
模型压缩、加速及移动端部署
When the win10 system is turned on, the response is slower than usual. How to solve it by turning around and waiting for 1 minute?
Grasp the development trend of robot education towards AI intelligence
Bryntum Scheduler Pro 5.0.6 \Gantt\Crack
UE4 设置夜晚(根据DirectionalLight方向更新SkySphere)
Large file slice upload and breakpoint continuation
Execute function semicolon immediately
通过删除注册表破解plsql
The Prospectus has written "yuancosmos" 318 times! Feitian Yundong fights Hong Kong stocks again "yuancosmos first share"“
ES6中的一些新特性
Random forest learning notes
把握机器人教育朝AI智能化发展的趋势
力扣每日一題-第41天-125. 驗證回文串
tensorflow 神经网络实现鸢尾花分类
How to solve the problem of win10? The app store cannot be opened, and the error code is 0x80131500
看融合数学教学的steam教育模式