javascript $.proxy() 和 bind() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12501231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:21:25  来源:igfitidea点击:

What is the difference between $.proxy() and bind()?

javascriptjqueryscopeclosuresbind

提问by Max Cantor

In 2009, ECMAScript 5 added a built-in bind()function which takes an object as a parameter and returns an identical function in which thiswill always refer to the object you passed it. (I couldn't find anything that looked like a canonical documentation link.)

在 2009 年,ECMAScript 5 添加了一个内置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)?

这与jQuery 的$.proxy()函数有何不同?是不是$.proxy()在 ECMAScript 5 发布之前就出现了?是否有特别的原因,有利于$.proxy(function(){}, this)function(){}.bind(this)

回答by Matt Whipple

proxycame first and you should likely favor bindas it is a standard. The way they are called varies slightly (due to being attached to Function.prototypevs just being a function) but their behavior is the same.

proxy排在第一位,你应该会喜欢bind它,因为它是一个标准。它们的调用方式略有不同(由于附加到Function.prototype函数与只是一个函数),但它们的行为是相同的。

There is a pretty good post here: jQuery.proxy() usage, that ends with that advice.

这里有一篇很好的帖子:jQuery.proxy() 用法,以该建议结尾。

回答by Norguard

Edit

编辑

Please pay no attention to this post (despite being the accepted answer).
Long story short, it was my own fault for making assumptions about the context of the question, rather than just looking up the API docs, and was accepted as the answer before I could realize my own stupidity (making assumptions, without validating them) and delete it.

请不要关注这篇文章(尽管是公认的答案)。
长话短说,对问题的上下文做出假设是我自己的错,而不仅仅是查找 API 文档,并且在我意识到自己的愚蠢之前被接受作为答案(做出假设,而不验证它们)和删除它。

Matt Whipple's answer is 100% correct, and while I disagree with his statement that real Proxies are useless in JS (they would be fantastic in some low-level concerns), the rest of his statements are flat-out objectively correct (aside from actual dates for .bindvs .proxy, as .bindwas in the spec years before it landed consistently in browsers).

Matt Whipple 的回答是 100% 正确的,虽然我不同意他关于真正的代理在 JS 中没用的说法(它们在一些低级问题中会很棒),但他的其余陈述在客观上是完全正确的(除了实际.bindvs 的日期.proxy,就像.bind它在浏览器中一致登陆之前的规范年份一样)。

Below is my shame, in the stocks for all to see...

下面是我的耻辱,在股票中给大家看...

Feel free to throw tomatoes at it.
If you want to know why I answered the way I did, read the comments below.

随意扔西红柿。
如果你想知道我为什么这样回答,请阅读下面的评论。



The difference between $({}).proxy()and func.bind({})is that proxy is a loose connection. You can detach at any time.

That's sort of what proxies are for. The invisible-interface between what you want to do and the thing that will actually do it.

For the record, there's also a $.bind()which is not a proxy. That is to say, it fully binds to this, in the same way that func.bind()does, rather than implementing a mediator-system to attach and detach context from functions at-will.

$({}).proxy()和之间的区别在于func.bind({})代理是松散连接。您可以随时分离。

这就是代理的用途。你想做的事和实际要做的事之间的隐形界面。

作为记录,还有一个$.bind()不是代理。也就是说,它this以与 相同的方式完全绑定到,func.bind()而不是实现一个中介系统来随意从函数中附加和分离上下文。

回答by abuduba

$.proxycame first. Below is a simple way to preserve a particular context on function call

$.proxy首先来了。下面是一种在函数调用时保留特定上下文的简单方法

var myProxy = (function(context,fn){
  return function(){
      fn.call(context);
  }
})( myContext, myFn );

You could easily use this before it came out jquery.

在 jquery 出现之前,您可以轻松地使用它。

Answer is simple: bindis the official. Use bind- if it really is supported in browsers which is required to run the script

答案很简单: bind是官方的。使用bind- 如果它确实在运行脚本所需的浏览器中受支持

回答by Benny Bottema

From Underscore bind vs jQuery.proxy vs Native bind

来自Underscore 绑定 vs jQuery.proxy vs Native 绑定

In addition to what is already mentioned, there's another difference between $.proxy()and .bind. Methods bound with $.proxy will return the same reference if called multiple times; jQuery caches functions proxied to an Object.

除了已经提到的内容之外,$.proxy()和之间还有另一个区别.bind。如果多次调用,与 $.proxy 绑定的方法将返回相同的引用;jQuery 缓存代理到对象的函数。

jsFiddle

js小提琴

回答by Morio

Here is a test you could try to for performance comparison.

这是您可以尝试进行性能比较的测试。

http://jsperf.com/bind-vs-jquery-proxy/5

http://jsperf.com/bind-vs-jquery-proxy/5

At this time, October 2014. The performance varies like crazy between browsers. IE 11 native bind is fastest.

此时,2014 年 10 月。浏览器之间的性能差异很大。IE 11 本机绑定是最快的。

However, for all three browsers I tested with, native bind out preform jquery proxy. And since bind() is standard, I would suggest sticking to it if possible.

但是,对于我测试过的所有三个浏览器,本机绑定了 preform jquery 代理。由于 bind() 是标准的,如果可能的话,我建议坚持使用它。