const fn = () => {}
基本上与旧的相同
"const fn = (function(){}).bind(this);
Function.prototype.bind 返回一个新函数,每次调用都会返回一个新的函数。因此,最好将绑定函数存储在某个地方,而不是每次遇到时创建它。
this
值。 - Fox Amadeusthis
,也无法绑定或传递任何上下文。而且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'})();
map
?我认为这不是重复的问题。这个问题是关于bind
和=>
的。 - The Reasonrender
函数中不应该做的事情(就像你不应该在那里调用bind
一样)。 - Bergi