【问题标题】:How can I call a high order function again in the code?如何在代码中再次调用高阶函数?
【发布时间】:2020-09-17 14:46:48
【问题描述】:
const x = { a:1 };

(function q({a}){
  console.log(a);
  return a;
})(x); // unable to execute

console.log(q(x))

我无法执行第 6 行。为什么?如果我删除第 6 行,则第 3 行控制台正在工作:

Error: q is not defined

【问题讨论】:

标签: javascript node.js function output javascript-objects


【解决方案1】:
const x = { a:1 };
// This IIFE isn't assigned to a variable in global. No reference to the object
(function q({a}){
  console.log(a);
  return a;
})(x); // unable to execute

控制台.log(q(x))

这可行,但你不能再次调用它,因为 a 被返回到 q 并且 q 不是一个函数:

const x = { a:1 };
// This IIFE isn't assigned to a variable in global. No reference to the object
const q = (function ({a}){
  console.log(a);
  return a;
})(x); // unable to execute



 console.log(q) // => 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2021-11-29
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多