(前端小白)最近在看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())
箭頭函數讓函數表達式更加簡潔