如何在代码中再次调用高阶函数?

问题描述 投票:0回答:1
const x = { a:1 };

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

console.log(q(x))

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

Error: q is not defined

javascript node.js function output javascript-objects
1个回答
0
投票
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

console.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)
© www.soinside.com 2019 - 2024. All rights reserved.