关闭或绑定 [英] Closure or Bind

查看:117
本文介绍了关闭或绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有限制1和限幅器2有什么区别?

Is there any difference between limiter 1 and limiter 2?

var limiter1 = function(limiter){
   return function(item){
      return item > limiter;
   };    
};
var limiter2 = function(limiter){
   return function(limiter,item){
      return item > limiter;
   }.bind(this,limiter);
};


推荐答案

在大多数情况下,它们将相同的功能。但是...

In most cases, they will function identically. However...

如果你曾经开始实际使用的这个,由 limiter1 返回的功能将未绑定的值(因此消费者可以一起 Function.prototype.bind )的调用更改值。在 limiter2 ,它锁定在初始绑定电话。

If you ever start to actually use the value of this, the function returned by limiter1 will be unbound (so a consumer could change the value with a call to Function.prototype.bind). In limiter2, it's locked down with the initial bind call.

另外,他们使用不同等级的范围,以获得限制变量。根据不同的引擎,你可以在性能(分钟)的区别。

Also, they use different levels of scope to get the limiter variable. Depending on the engine, you could have a (minute) difference in performance.

这篇关于关闭或绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆