简体   繁体   English

函数式编程:使用闭包和使用bind方法有什么区别?

[英]Functional Programming: What's the difference between using a closure and using the bind method?

I'm working on a tutorial that explains functional programming. 我正在研究一个解释函数式编程的教程。 He asked me to come with a solution and it worked, but his solution uses the .bind method of the function. 他要求我提供一个解决方案,它可以工作,但是他的解决方案使用函数的.bind方法。

Is there a difference between our solutions other than the syntax? 除语法外,我们的解决方案之间是否还有其他区别?

function mapForEach(arr, fn) {
  var newArr = [];
  for(var i = 0; i < arr.length; i++){
    newArr.push(fn(arr[i]));
  }
  return newArr;
}

var arr1 = [1,2,3,4,5];

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

var checkPassedLimitWithClosure = function(limiter){
  return function (item) {
    return item >= limiter;
  };
};

var notPassed3 = mapForEach(arr1, checkPassedLimitWithBind(3));
var doesNotPass3 = mapForEach(arr1, checkPassedLimitWithClosure(3));

alert(notPassed3);
alert(doesNotPass3);

The examples can be found here as well: https://jsfiddle.net/podbarron/73m86cj3/ 示例也可以在这里找到: https : //jsfiddle.net/podbarron/73m86cj3/

There is absolutely no behavior difference because that function does not use this . 绝对没有行为差异,因为该函数不使用this

Otherwise it would be different, yes: 否则会有所不同,是的:

 var checkPassedLimitWithBind = function(limiter) { return function (limiter, item) { return this == item; }.bind(this, limiter); }; var checkPassedLimitWithClosure = function(limiter) { return function (item) { return this == item; }; }; console.log( checkPassedLimitWithBind.call(123)(123) ); // true console.log( checkPassedLimitWithClosure.call(123)(123) ); // false 

The bind solution is unnecessarily complicated. bind解决方案不必要地复杂。 There's no need to partially apply the limiter value there since the function can pretty much access it directly. 由于该函数几乎可以直接访问它,因此无需在此部分应用limiter值。

Both will end up working the same way. 两者最终将以相同的方式工作。 However, it can be different if the variable ever gets reassigned (you'd never want to do it with function arguments). 但是,如果重新分配了变量,则可能会有所不同(您永远都不想使用函数参数来执行此操作)。

var checkPassedLimitWithBind = function(limiter){
  var fn = function (limiter, item) {
    return item >= limiter;
    //limiter === argument value for limiter parameter
  }.bind(this, limiter);
  limiter = 5;
  return fn;
};

var checkPassedLimitWithClosure = function(limiter){
  var fn = function (item) {
    return item >= limiter;
    //limiter === 5 all the time
  };
  limiter = 5;
  return fn;
};

Answering the post title: Basically, the closure will have access to whatever value that reference is holding. 回答帖子标题:基本上,闭包将可以访问引用所拥有的任何值。 When you bind the function you'll get that specific value passed down. 当您绑定函数时,您将获得该特定值的传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用无状态功能组件与调用方法有什么区别? - What's the difference between using a stateless functional component versus calling a method? Javascript“Promises”和函数式编程的“任务”之间有什么区别? - What's the Difference Between Javascript “Promises” and functional Programming's “Task”? 使用函数式编程复制数组的元素 - Duplicating an array's elements using functional programming 绑定返回和使用function()的结果有什么区别 - What is the difference between the return from bind and the result of using function() 两种使用闭包的方式之间的区别 - Difference between two ways of using a closure 使用jQuery和它的别名($)有什么区别? - What is the difference between using jQuery and it's alias ($)? 在参数中使用 then 和 not 有什么区别 - what's the difference between using then in argument and not PolymerJS中的bind()和bindProperty()之间有什么区别? - What's the difference between bind() and bindProperty() in PolymerJS? 直接函数调用和闭包中的call()apply()之间的区别是什么 - what's the difference between direct function call and call() apply() in closure 内存泄漏关闭和简单的解决方法之间有什么区别? - What's the difference between a memory leaking closure and its easy workaround?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM