箭头函数的定义与调用
(前端小白)最近在看es6语法,所以简单的做下笔记,记下箭头函数的定义与调用方式
1.无参数的箭头函数
const test = () => 1
相当于
function test(){
return 1
}
2.有参数的箭头函数
const test=(x,y)=>x+y
相当于
function test(x, y) {
return x + y
}
2.1箭头函数返回一个对象时,return 部分需要加个大括号如下:
const test = (
name,
age
) => ({
name: name,
age: age
});
test('zs',3)
2.3箭头函数简化回调函数
function test() {
setInterval(() => {
console.log('箭头函数')
}, 2000)
}
console.log(test())
箭头函数让函数表达式更加简洁