Search code examples
javascriptecmascript-6iife

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

In the IIFE initialize parkReport.averageAge() is showing an error of not a function. How do you call the nested AverageAge() from initialize?


Solution

  • You need to call the parkReport function. Here you are passing parkReport as a callback to the IIFE. so you need to call it to expect something returned from it.

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