作为参数传递时如何计算函数

How Are Functions Evaluated When Passed As Arguments

本文关键字:计算 函数 何计算 参数传递      更新时间:2023-09-26

在Javascript中,函数是'一等公民'。但是,我对将它们作为参数传递给函数时如何计算有点困惑。

const childFunction = () => (
...
);
const parentFunction = ( childFunction ) =>(
...
);

我想知道代码流的顺序是什么。所以会是这样的:

"parentFunction"被执行。参数"childFunction"被标识为参数,"childFunction"被执行。一旦从"子函数"收到结果,那么"父函数"的主体就会被执行?

谢谢

childFunction不是通过作为参数传递来执行的。接受childFunction的函数必须使用childFunction()childFunction.apply/call调用它

const childFunction = () => (
...
);
const parentFunction = ( childFunction ) =>(
   // childFunction doesn't get called/executed until this line is reached
   childFunction();
   // Or something like
   childFunction.call(this, 1,2,3);
...
);