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

How to make a nested function call within an IIFE?

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

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

您需要调用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);