如何在IIFE中进行嵌套函数调用?

时间:2017-11-23 10:27:47

标签: javascript ecmascript-6 iife

const parkReport = () => {
     return {
       averageAge: () => {
          return console.log('Something');   
      }
     } 
    };
    const initialize = ( parkReport => {
      return parkReport.averageAge();
    })(parkReport);

在IIFE initialize parkReport.averageAge()中显示的错误不是函数。如何从AverageAge()调用嵌套initialize

1 个答案:

答案 0 :(得分:0)

您需要调用parkReport函数。在这里,您将parkReport作为callback传递给IIFE。所以你需要call它才能期待从它返回的内容。

const parkReport = () => {
  return {
    averageAge: () => {
      return console.log('Something');
    }
  }
};
const initialize = (parkReport => {
  return parkReport() // call it 
    .averageAge(); // and get the age
})(parkReport);