![](/img/trans.png)
[英]Calling regular function vs. arrow function before their declaration in a class
[英]Arrow function vs. bound function
const fn = () => {}
與舊的基本相同
const fn = (function(){}).bind(this);
Function.prototype.bind在每次調用時返回一個新函數。 這就是為什么在某個地方存儲綁定函數而不是在每次遭遇時創建它的好主意。
它屬於一個事實,即箭頭功能不能使用this
你可能會從其他的東西知道它的方式。 您不能將任何上下文綁定或傳遞給箭頭函數。 並且該bind
始終會創建函數對象的副本。
這一起解釋了文章中的示例。 首先,他們使用一個正常的功能,能夠綁定this
吧。 在第二步中,它們使用具有正確綁定的function
的新副本覆蓋原始function
對象本身。 這是一個巨大的性能提升,因為你只需要做一次,而不是將來的任何電話。
this
問題的一個例子:
function testNormal() { // 'this' is the object, passed below by 'bind' console.log(this); } var testArrow = () => { // 'this' is always the current parent // there is no way to pass an other value to 'this' in an arrow function console.log(this); } testNormal.bind({data: 'test'})(); testArrow.bind({data: 'test'})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.