$ .proxy()和bind()有什么区别? [英] What is the difference between $.proxy() and bind()?
问题描述
2009年,ECMAScript 5添加了内置的bind()
函数,该函数将对象作为参数并返回相同的函数,其中this
将始终引用传递给它的对象. (我找不到任何看起来像是规范文档链接的东西.)
In 2009, ECMAScript 5 added a built-in bind()
function which takes an object as a parameter and returns an identical function in which this
will always refer to the object you passed it. (I couldn't find anything that looked like a canonical documentation link.)
这与 jQuery的$.proxy()
函数有何不同? $.proxy()
在ECMAScript 5发布之前是否先出现?是否有特别的理由要使$.proxy(function(){}, this)
胜于function(){}.bind(this)
?
How is this different from jQuery's $.proxy()
function? Did $.proxy()
come first before ECMAScript 5 was released? Is there a particular reason to favor $.proxy(function(){}, this)
over function(){}.bind(this)
?
推荐答案
proxy
首先出现,您可能更喜欢bind
,因为它是标准.它们的调用方式略有不同(由于被附加到Function.prototype
而不是仅仅作为一个函数),但是它们的行为是相同的.
proxy
came first and you should likely favor bind
as it is a standard. The way they are called varies slightly (due to being attached to Function.prototype
vs just being a function) but their behavior is the same.
这里有一个很好的帖子: jQuery.proxy()用法,以该结尾建议.
There is a pretty good post here: jQuery.proxy() usage, that ends with that advice.
这篇关于$ .proxy()和bind()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!