简体   繁体   English

在函数内部返回函数是如何工作的

[英]How does returning a function inside a function work

I feel like my example explains it well.我觉得我的例子解释得很好。 I am unsure of how a function returning inside a function works.我不确定在函数内部返回的函数是如何工作的。 I feel like solving the below demo would help me understand.我觉得解决下面的演示会帮助我理解。 Any other links or documents would be nice to read though as well.任何其他链接或文档也很好阅读。

function LogFunction() {
  return function() {
    return "just console log this";
  };
}

console.log(LogFunction());
// "just console log this"   <<< I want this
// "[Function]"              <<< I am getting this

You are returning the reference of the inner function, to execute it you need to put () again so that the function reference is executed.您正在返回内部函数的引用,要执行它,您需要再次 put ()以便执行函数引用。

 function LogFunction() { return function() { return "just console log this"; }; } console.log(LogFunction()()); //You can also get the function reference in a variable, and execute it: const innerFunction = LogFunction(); //assigning the returned function reference. console.log(innerFunction()); //executes the returned reference

In JavaScript functions are known as "First-class function" this means you can return / pass functions as any other type like (object / numbers).在 JavaScript 中,函数被称为“一等函数”,这意味着您可以像任何其他类型(对象/数字)一样返回/传递函数。 You can find more about this here .您可以在此处找到更多相关信息

You can break this one into variables to make it more clear such as:您可以将其分解为变量以使其更加清晰,例如:

function LogFunction() {
  return function() {
    return "just console log this";
  };
}

var a = LogFunction()
console.log( a() );

However as others pointed correctly you can call this directly但是,正如其他人指出的那样,您可以直接调用它

console.log( LogFunction()() );

On the variables examples, when a LogFunction() is called with () you are invoking the function and the result of this invocation is assigned to the variable a , as the result of invoking the function is another function you need to call the result of the previous function in order to access the result of this function a() .在变量的例子,当LogFunction()被调用, ()要调用的功能,这个调用的结果赋值给变量a ,因为调用函数的结果是另一个函数,你需要调用的结果前一个函数,以便访问此函数a()的结果。

You can have as many levels of nested function as you wish, and with the new ES2016 you can take advantage of arrow functions to make this code much clear.您可以根据需要拥有任意多级别的嵌套函数,并且在新的 ES2016 中,您可以利用箭头函数使此代码更加清晰。

const LogFunction = () => () => "just console log this";
console.log( LogFunction()() );
console.log((LogFunction())());

LogFunction returns a function. LogFunction返回一个函数。 So LogFubction() is a function.所以LogFubction()一个函数。 You just call it你就叫它

Your function LogFunction returns an internal function that you also need to call to get the final result.您的函数 LogFunction 返回一个内部函数,您还需要调用该函数来获取最终结果。

 function LogFunction() { return function() { return "just console log this"; }; } console.log(LogFunction()());

You can use less code:您可以使用更少的代码:

 function LogFunction() { return "just console log this"; } console.log(LogFunction())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM