简体   繁体   English

重复回调函数的最佳实践?

[英]Best practice for repeating callback functions?

In my code I use a lot of (named) callback functions, just to give a quick example: 在我的代码中,我使用了很多(命名的)回调函数,只是为了给出一个简单的例子:

function showThis(callback) {

  // Do something

  if (callback && typeof(callback) === 'function') {
    callback();
  }

}

Now I have this pattern repeat throughout different functions (I'm talking about the callback part), so is it considered to be better if I make one generic callback handler function and include that? 现在我在不同的函数中重复这个模式(我在谈论回调部分),所以如果我创建一个通用的回调处理函数并包含它,它会被认为更好吗?

Something like: 就像是:

function doCallback(callback) {

  if (callback && typeof(callback) === 'function') {
    callback();
  }

}

function showThis(callback) {

  // Do something

  doCallback(callback);
}

I would think that's better to keep code DRY, but I'm unsure. 我认为保持代码DRY更好,但我不确定。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

In my opinion it's not necessary to have a callback handler. 在我看来,没有必要有一个回调处理程序。 In different situations you may need to call the callback with different context (for example callback.call(ob... or callback.apply(obj... ). So you will need an extra parameter of the callback handler (the context). Another thing that isn't very pleasantly is that you may need to pass custom arguments to the callback. With the callback handler you can make the pain smaller by passing all the parameters into an array and applying the function on them. Something like...: 在不同的情况下,您可能需要使用不同的上下文调用回调(例如callback.call(ob...callback.apply(obj... )。所以您需要一个额外的回调处理程序参数(上下文)另一件不太愉快的事情是你可能需要将自定义参数传递给回调。使用回调处理程序,你可以通过将所有参数传递给数组并在其上应用函数来减轻痛苦。 ..:

function callbackHandler(callback, arguments, context) {
    if (typeof callback === 'function') {
        return callback.apply(context, arguments);
    }
    return null;
}

But there're so many optional parameters... Another thing is the check which you are doing: 但是有很多可选参数......另一件事就是你正在做的检查:

if (callback && typeof callback === 'function') //notice that typeof is an operator not a function, so you don't need parentless

is not actually necessary, this is enough: typeof callback === 'function' 实际上并不是必需的,这就足够了: typeof callback === 'function'

The first condition will return false only if callback is evaluated to false but if it's evaluated to false typeof callback wont return function . 只有当callback被评估为false但是如果它被评估为false typeof callback而不返回function ,第一个条件将返回false So the condition is short enough. 所以条件足够短。

That's why I think that you don't need a callback handler. 这就是为什么我认为你不需要回调处理程序。 And by the way nice question I love such topics! 顺便问一下我喜欢这样的话题! :-) :-)

Yes, that might well be a good idea. 是的,这可能是一个好主意。 Like you said, it avoids repeating yourself, and it's perfectly clear as to its intent. 就像你说的那样,它避免重复你自己,并且它的意图非常明确。 While you do lose access to the variables that were in the showThis scope, you were losing that anyway because callback wasn't defined there, so there's nothing to worry about. 虽然您确实无法访问showThis范围内的变量,但无论如何都会丢失,因为callback没有在那里定义,因此没有什么可担心的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM