71

我想在 JavaScript 中编写自己的函数,该函数将回调方法作为参数并在完成后执行它,我不知道如何在我的方法中调用作为参数传递的方法。像反射一样。

示例代码

function myfunction(param1, callbackfunction)
{
    //do processing here
    //how to invoke callbackfunction at this point?
}

//this is the function call to myfunction
myfunction("hello", function(){
   //call back method implementation here
});
4

8 回答 8

103

您可以将其称为普通函数:

function myfunction(param1, callbackfunction)
{
    //do processing here
    callbackfunction();
}

唯一额外的就是提到context。如果您希望能够this在回调中使用关键字,则必须分配它。这通常是可取的行为。例如:

function myfunction(param1, callbackfunction)
{
    //do processing here
    callbackfunction.call(param1);
}

在回调中,您现在可以访问param1as this。见Function.call

于 2011-05-14T10:16:59.710 回答
8

我也遇到了同样的情况,我必须将作为参数发送的函数调用给另一个函数。

我试过了

mainfunction('callThisFunction');

第一种方法

function mainFuntion(functionName)
{
    functionName();
}

但以错误告终。所以我尝试了

第二种方法

functionName.call(). 

还是没用。所以我尝试了

第三种方法

this[functionName]();

这就像一个冠军。所以这只是增加了一种调用方式。可能我的第一种和第二种方法可能存在问题,而是更多地使用谷歌搜索并花时间选择第三种方法。

于 2014-12-10T06:49:51.830 回答
2
function myfunction(param1, callbackfunction)
{
    //do processing here
   callbackfunction(); // or if you want scoped call, callbackfunction.call(scope)
}
于 2011-05-14T10:17:19.520 回答
1

object[functionName]();

object:指对象的名称。

functionName:是一个变量,我们将使用其值来调用函数。

通过将用于引用函数名的变量放在[]内和括号外的()中,我们可以使用该变量动态调用对象的函数。点表示法不起作用,因为它认为 'functionName' 是函数的实际名称,而不是 'functionName' 持有的值。这让我有点疯狂,直到我遇到了这个网站。我很高兴 stackoverflow.com 存在 <3

于 2017-09-21T02:39:56.267 回答
1

这里的所有示例似乎都显示了如何声明它,而不是如何使用它。我认为这也是@Kiran 有这么多问题的原因。

诀窍是声明使用回调的函数:

function doThisFirst(someParameter, myCallbackFunction) {
    // Do stuff first
    alert('Doing stuff...');

    // Now call the function passed in
    myCallbackFunction(someParameter);
}

someParameter如果不需要,该位可以省略。

然后,您可以按如下方式使用回调:

doThisFirst(1, myOtherFunction1);
doThisFirst(2, myOtherFunction2);

function myOtherFunction1(inputParam) {
    alert('myOtherFunction1: ' + inputParam);
}

function myOtherFunction2(inputParam) {
    alert('myOtherFunction2: ' + inputParam);
}

注意回调函数是如何传入和声明的,没有引号或括号

  • 如果你使用doThisFirst(1, 'myOtherFunction1');它会失败。
  • 如果您使用doThisFirst(1, myOtherFunction3());(我知道在这种情况下没有参数输入),那么它将myOtherFunction3 首先调用,因此您会得到意想不到的副作用。
于 2016-12-01T18:05:42.250 回答
0

另一种方法是将您的函数声明为匿名函数并将其保存在变量中:

var aFunction = function () {
};

之后,您可以将 aFunction 作为参数 myfunction 传递并正常调用它。

function myfunction(callbackfunction) {
    callbackfunction();
}

myfunction(aFunction);

但是,正如其他答案所指出的那样,这不是必需的,因为您可以直接使用函数名称。由于评论中的讨论,我将保持原样。

于 2011-05-14T10:18:46.707 回答
0

我会做这样的事情

var callbackfunction = function(param1, param2){
console.log(param1 + ' ' + param2)
}

myfunction = function(_function, _params){
_function(_params['firstParam'], _params['secondParam']);
}

进入主代码块,可以传参数

myfunction(callbackfunction, {firstParam: 'hello', secondParam: 'good bye'});
于 2016-09-16T21:57:15.943 回答
-1

基于上面的一些优秀答案和资源,我的用例的超级基本实现:

/** Returns the name of type member in a type-safe manner. **(UNTESTED)** e.g.:
*
* ```typescript
* nameof<Apple>(apple => apple.colour); // Returns 'colour'
* nameof<Apple>(x => x.colour); // Returns 'colour'
* ```
*/
export function nameof<T>(func?: (obj: T) => any): string {
 const lambda = ' => ';
 const funcStr = func.toString();

 const indexOfLambda = funcStr.indexOf(lambda);
 const member = funcStr.replace(funcStr.substring(0, indexOfLambda) + '.', '').replace(funcStr.substring(0, indexOfLambda) + lambda, '');

 return member;
}
于 2020-02-07T04:42:35.080 回答