【问题标题】:Nested function in JavascriptJavascript中的嵌套函数
【发布时间】:2022-10-21 20:17:07
【问题描述】:

下面是我用 Javascript 编写的简单嵌套函数

function outside(x) {
    function inside(y) {
      
      console.log(x + y);
    }
   return inside ;     //
  }
  const fnInside = outside(3); 
  const result = fnInside(5);

程序运行正常。但是,如果我从第 5 行删除 return 语句,则会引发以下错误。

谁能向我解释为什么return 语句删除会使程序表现得像这样?

谢谢你

【问题讨论】:

    标签: javascript function nested return


    【解决方案1】:

    如果删除return,您将不会返回function 引用,外部函数将是void 函数,默认情况下将返回undefined,当您尝试将undefined 作为函数执行时,它会给出一个错误。

    const undefinedValue = undefined;
    
    undefinedValue()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-12
      • 2013-11-15
      • 2019-05-23
      • 2012-02-07
      • 1970-01-01
      • 2011-03-31
      相关资源
      最近更新 更多