作为参数传递时如何计算函数
How Are Functions Evaluated When Passed As Arguments
在Javascript中,函数是'first class citizens'。但是,我对将它们作为参数传递给函数时如何求值感到有点困惑。
const childFunction = () => (
...
);
const parentFunction = ( childFunction ) =>(
...
);
我想知道代码流的顺序是什么。那么它会是这样的吗:
'parentFunction' 被执行。参数 'childFunction' 被识别为参数, 'childFunction' 被执行。一旦从 'childFunction' 收到结果,然后执行 'parentFunction' 的主体?
谢谢,
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);
...
);
在Javascript中,函数是'first class citizens'。但是,我对将它们作为参数传递给函数时如何求值感到有点困惑。
const childFunction = () => (
...
);
const parentFunction = ( childFunction ) =>(
...
);
我想知道代码流的顺序是什么。那么它会是这样的吗:
'parentFunction' 被执行。参数 'childFunction' 被识别为参数, 'childFunction' 被执行。一旦从 'childFunction' 收到结果,然后执行 'parentFunction' 的主体?
谢谢,
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);
...
);