【问题标题】:How to make a nested function call within an IIFE?如何在 IIFE 中进行嵌套函数调用?
【发布时间】:2017-11-23 10:27:47
【问题描述】:
const parkReport = () => {
     return {
       averageAge: () => {
          return console.log('Something');   
      }
     } 
    };
    const initialize = ( parkReport => {
      return parkReport.averageAge();
    })(parkReport);

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

【问题讨论】:

    标签: javascript ecmascript-6 iife


    【解决方案1】:

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

    【讨论】:

    • 请不要“戳”OP。如果他们喜欢,他们会accept
    • 那么对于像parkReport().averageAge().sumofAges()这样的每个连续呼叫都必须这样做吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多