【问题标题】:How does the call pass its argument to the inner function? [duplicate]调用如何将其参数传递给内部函数? [复制]
【发布时间】:2022-01-22 03:55:27
【问题描述】:

参数 10 绕过外部函数传递给匿名内部函数。这里的原理是什么?

function aFunc() {
  let firstNum = 2;
  return (secondNum) => secondNum * firstNum;
}
let aVar = aFunc();
console.log(aVar(10));

【问题讨论】:

    标签: javascript scope closures


    【解决方案1】:

    因为aVar(secondNum) => secondNum * firstNum,其中firstNum 和闭包有firstNum = 2

    function aFunc() {
      let firstNum = 2;
      return (secondNum) => secondNum * firstNum;
    }
    let aVar = aFunc();
    

    调用aFunc() 运行在firstNum 周围创建闭包并返回箭头函数(secondNum) => secondNum * firstNum 的函数。

    后续对aVar(10)的调用调用此箭头函数,计算并返回firstNum(2)和secondNum(10)相加的结果。

    console.log(aVar(10));
    

    【讨论】:

    • 这不是骗子,因为...?
    猜你喜欢
    • 2016-01-11
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2022-11-10
    • 2014-05-22
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多