簡體   English   中英

我們如何將 function 的一個特定變量作為回調傳遞?

[英]How can we pass one specific variable of a function as a callback?

我最近拿起了回調函數並試圖了解更多關於它的信息。 我想從 function y 傳遞 c ,這是 function x 作為參數的最終總和。 這可以做到嗎?怎么做?

function x(y){
    console.log("x");
     y();
}

x(function y(){
    var a = 5;
    var b = 45;
    var c = a+b;
    console.log(c);
});

您可以簡單地從 y 返回 c 並在 x 中使用它,如下所示:

function x(y){
    console.log("x");
    let c = y();
    console.log("c: ", c);
}

x(function y(){
    var a = 5;
    var b = 45;
    var c = a+b;
    console.log(c);
    return c;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM