如何实现可重用的回调函数 [英] How to implement reusable callback functions

查看:97
本文介绍了如何实现可重用的回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript的新手,我在节点工作,需要很好地理解异步编程和回调设计。我发现即使你的回调是多层深度,使用嵌入式函数也很容易。你的嵌入式回调最终会被关闭。

I am fairly new to JavaScript and I am working in node which requires a good understanding of async programming and callback design. I have found that using embedded functions is very easy to do even when your callbacks are multiple levels deep. Your embedded callbacks just end up being closures.

但是,如果你有几层回调,其中许多回调在执行路径上是相似的,你最终会重写很多在单独的回调链中反复使用回调代码。例如,如果下面的mycb1和mycb2定义移到A之外,它们就不再具有对A变量的隐式访问权限,因此不再用作闭包。

However, when you have several layers of callbacks where many of the callbacks are similar across execute routes, you end up rewriting a lot of callback code over and over in separate callback chains. For example, if mycb1 and mycb2 definitions below are moved outside of A, they no longer have implicit access to A's variables and thus, no longer function as closures.

嵌入式定义示例,它们用作闭包。

Example with embedded definitions where they function as closures.

mod.A=function(){
  var mycb1=function(err){
    if (!err){
      var mycb2=function(err){
        cb(err);
      };
      mod.foo2(args,mycb2);
    }
    else{cb(err);}
  };
  mod.foo1(args,mycb1);
}

mod.foo1 = function(args,cb){
  //do some work
  cb(err);
};

mod.foo2 = function(args,cb){
  //do some work
  cb(err);
} 
//execute
mod.A();

我想执行以下操作,但能够更改mycb1和mycb2函数的变量范围,以便它们能够可以用作从哪里调用它们的闭包。
例如:

I want to do the following but be able to change the variable scope for mycb1 and mycb2 functions so they can be used as closures from where ever they are called. For example:

var mycb2=function(err){
  ...
  cb(err);
};

var mycb1=function(err){
  if (!err){        
    mod.foo2(args,mycb2);  //but have it act like a closure to mycb1
  }
  else{cb(err);}
};

mod.A=function(){
  ...
  mod.foo1(args,mycb1);  //but have it act like a closure to A
} 

mod.foo1 = function(args,cb){
  //do some work
  cb(err);
}

mod.foo2 = function(args,cb){
  //do some work
  cb(err);
}

我知道我可以实现一个在mod级别设置变量的设计所以他们可以访问mod级别的功能。然而,这似乎在某种程度上污染了具有变量的mod范围,这些变量可能仅被其一些方法使用。我也知道我可以传入变量,使它们在执行时可以被回调访问。但是,如果我理解JS和回调如何工作,我将不得不将它们传递给fooX然后让foo将它们传递给回调。这似乎也不是一个好计划。
函数的变量范围是否可以改变,因此它从执行点而不是它的定义点就像一个闭包?如果没有,模块化你的回调代码的最佳方法是什么,以便可以重复使用?

I know that I can implement a design that either sets variable at the mod level so they are accessible to mod level functions. However, this seems to somewhat pollute the mod scope with variable that may only be used by a few of its' methods. I also know that I could pass in variables to make them accessible to the callbacks when they are executed. However, if I understand how JS and callbacks work, I would have to pass those to fooX and then have foo pass them to the callbacks. That doesn't seem like a good plan either. Can the variable scope of a function be changed so it acts like a closure from its point of execution rather than its point of definition? If not, what is the best way to modularize your callback code so it can be reused?

推荐答案

一般来说,没有绕过必须创建另一个有权访问闭包的函数 in-line 。您可以通过一个简单的匿名函数来创建函数,该函数将一些参数传递给父回调作为参数,同时接受其余的(即部分函数),或者使用 Function.bind()创建部分函数本身。

In general, there is no getting around having to create another function in-line that has access to the closures. You can create the function in-line by having a simple anonymous function that passes some arguments to the parent callback as parameters while accepting the rest (i.e. a partial function), or by using Function.bind() to create the partial function itself.

例如,如果您最初有:

function(...) {
    // closure vars x1, y1
    foo.bar( function(result) {
        // do something with x1 and y1
    });
}

您可以将其提取到:

var callback = function(x1, y1, result) {
    // do something with x1 and y1
};

function(...) {
    // closure vars x1, y1

    // option 1: make your own partial function
    foo.bar( function(result) { return callback(x1, y1, result); });
    // with ES6: foo.bar( (result) => callback(x1, y1, result); });

    // option 2: use Function.bind
    foo.bar( callback.bind(this, x1, y1); );
}

这篇关于如何实现可重用的回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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