多個(gè)參數(shù)寫法
const? fn? =? (a1,a2)? =>{
????return a1 + a2;
}
如果函數(shù)內(nèi)部只有一行代碼可以這樣寫? 默認(rèn)返回 a1 + a2;
const? fn? =? (a1,a2)? =>? a1 + a2;
一個(gè)參數(shù) 寫法
const? fn? =? a1? =>{? // a1 可以不加 ()
? ? return a1;
}
沒有參數(shù)寫法
const? fn? =? ()? =>{
? ? console.log(" hello word ")
}
const?obj?=?{?????
????fn:(a1,a2)=>{???????
????console.log(this);???????
????return?a1*a2;????
?},?????
????add(a){? ? ? ?
? ? console.log(this);?
? ? ?return?a;????
}???
}???
console.log(obj.fn(5,5));? ?// fn的 this 指向 widow
console.log(obj.add(100)); // add的 this 指向 obj
const?obj?=?{?????
aaa(){?????
setTimeout(function({????????
console.log(this);??//window 用這種方式寫是call調(diào)用在傳this進(jìn)去庶骄,始終指向window
},?10);??????
setTimeout(()?=>?{??????????
console.log(this);?//obj? 寫成箭頭函數(shù),他是向外層作用域查找this這個(gè)變量,this => obj
},?10);?????}???
}???
obj.aaa();