Javascript 一步定义和调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7498361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:31:01  来源:igfitidea点击:

Defining and calling function in one step

javascriptrecursionfunctional-programmingiife

提问by quis

Is there a way in Javascript to define a function and immediately call it, in a way that allows it to be reused?

在 Javascript 中有没有一种方法可以定义一个函数并立即调用它,以一种允许它被重用的方式?

I know you can do one-off anonymous functions:

我知道您可以执行一次性匿名函数:

(function(i) {
    var product = i * i;
    console.log(product);
    // Can't recurse here because there's no (ECMA standard) way for the 
    // function to refer to itself
}(2)); // logs 4

Or you can name a function then call it afterwards:

或者你可以命名一个函数然后调用它:

function powers(i) {
    var product = i * i;
    console.log(i * i);
    if (product < 1e6) { powers(product) };
}

powers(2); // Logs 4, 16, 256...

But is there a cleaner way of defining and calling a function in one go? Sort of like a hybrid of both examples?

但是有没有一种更简洁的方法来一次性定义和调用一个函数?有点像这两个例子的混合体?

Not being able to do this isn't preventing me from doing anything, but it feels like it would be a nice expressive way to write recursive functions or functions that need to be run on $(document).ready()but also later when situations change, etc.

无法做到这一点并不能阻止我做任何事情,但感觉这将是一种很好的表达方式来编写递归函数或需要运行的函数,$(document).ready()但在情况发生变化时也会稍后运行,等等。

采纳答案by Marc Uberstein

You can try:

你可以试试:

(window.powers = function(i) {
  /*Code here*/
  alert('test : ' + i);
})(2);
<a href="#" onclick="powers(654)">Click</a>

Working link : http://jsfiddle.net/SqBp8/

工作链接:http: //jsfiddle.net/SqBp8/

It gets called on load, and I have added it to an anchor tagto change the parameter and alert.

它在加载时被调用,我已将其添加到 ananchor tag以更改参数和alert.

回答by Matthias Benkard

If all you want is access the function within its own body, you can simply specify a name after the functionkeyword:

如果您只想在函数体内访问该函数,只需在function关键字后指定一个名称即可:

> (function fac (n) {
    return (n === 0 ? 1 : n*fac(n-1));
  })(10)
3628800

This is a standard feature (see ECMA-262, ed. 5.1, p. 98).

这是一个标准特性(参见ECMA-262,第 5.1 版,第 98 页)。

回答by gkoberger

All the answers here are close to what you want, but have a few problems (adding it to the global scope, not actually calling it, etc). This combines a few examples on this page (although it unfortunately requires you to remember arguments.callee):

这里的所有答案都接近您想要的,但有一些问题(将其添加到全局范围,而不是实际调用它等)。这结合了此页面上的一些示例(尽管不幸的是需要您记住arguments.callee):

var test = (function() {
  alert('hi');
  return arguments.callee;
})();

Later, you can call it:

稍后,您可以调用它:

test();

回答by tristan

If you don't care about the return value, you can do this.

如果你不关心返回值,你可以这样做。

var powers = function powers(i) {
    var product = i * i;
    console.log(i * i);
    if (product < 1e6) { powers(product) };
    return powers;
}(2);