2016-04-26 91 views
-1

在Javascript中,函数是'头等公民'。但是,当我作为参数传递给一个函数时,我对它们的评估方式有些困惑。当通过参数时函数是如何评估的

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

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

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

'parentFunction'被执行。参数'childFunction'被识别为一个参数,'childFunction'被执行。一旦从'childFunction'收到结果,然后'parentFunction'的主体被执行?

感谢,

+1

叫它'childFunction'将仅当'parentFunction'执行它执行。它不会隐式执行。 – 2016-04-26 14:51:30

+1

@squint谢谢你,这很有道理。 – Kayote

回答

2

childFunction没有被作为一个参数只会传递执行。这需要childFunction功能具有使用childFunction()或​​

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); 
... 
);