这是我工作的简单示例示例:

function some_function(arg1, arg2, callback) {
    var my_number = Math.ceil(Math.random() *
        (arg1 - arg2) + arg2);
    callback(my_number);
}

some_function(5, 15, function(num) {
    console.log("callback called! " + num);
});


我试图不使用匿名功能。这是我尝试的错误:

function some_function(arg1, arg2, callback) {
    var my_number = Math.ceil(Math.random() *
        (arg1 - arg2) + arg2);
    callback(my_number);
}

some_function(5, 15, some_other_function(num));

function some_other_function(theNumber){
    console.log('printing number: ' + theNumber);
}


这有可能吗?如果是这样,怎么办?

最佳答案

您的callback(my_number);将使用参数some_other_function调用my_number

你很亲近

function some_function(arg1, arg2, callback) {
    var my_number = Math.ceil(Math.random() *
        (arg1 - arg2) + arg2);
    callback(my_number);
}

some_function(5, 15, some_other_function);

function some_other_function(theNumber){
    console.log('printing number: ' + theNumber);
    document.write('printing number: ' + theNumber);
}

关于javascript - 如何将node.js回调定向到非匿名函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35436170/

10-09 19:09