JavaScript 函数可以返回自身吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6959426/
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
Can a JavaScript function return itself?
提问by fbas
Can I write a function that returns iteself?
我可以编写一个返回 itself 的函数吗?
I was reading some description on closures - see Example 6- where a function was returning a function, so you could call func()();
as valid JavaScript.
我正在阅读一些关于闭包的描述——参见示例 6——其中一个函数正在返回一个函数,因此您可以func()();
作为有效的 JavaScript调用。
So I was wondering could a function return itself in such a way that you could chain it to itself indefinitely like this:
所以我想知道一个函数是否可以以这样的方式返回自己,你可以像这样无限期地将它链接到自己:
func(arg)(other_arg)()(blah);
Using arguments
object, callee or caller?
使用arguments
对象、被调用者还是调用者?
回答by Flambino
There are 2-3 ways. One is, as you say, is to use arguments.callee
. It might be the only way if you're dealing with an anonymous function that's not stored assigned to a variable somewhere (that you know of):
有2-3种方式。一种是,正如你所说,是使用arguments.callee
. 如果您正在处理未存储分配给某个变量的匿名函数(您知道),这可能是唯一的方法:
(function() {
return arguments.callee;
})()()()().... ;
The 2nd is to use the function's name
第二种是使用函数名
function namedFunc() {
return namedFunc;
}
namedFunc()()()().... ;
And the last one is to use an anonymous function assigned to a variable, but you have to know the variable, so in that case I see no reason, why you can't just give the function a name, and use the method above
最后一个是使用匿名函数赋值给一个变量,但是你必须知道这个变量,所以在那种情况下我看不出有什么理由,为什么你不能只给函数一个名字,并使用上面的方法
var storedFunc = function() {
return storedFunc;
};
storedFunc()()()().... ;
They're all functionally identical, but callee
is the simplest.
它们在功能上都相同,但callee
最简单。
Edit: And I agree with SLaks; I can't recommend it either
编辑:我同意 SLaks;我也不能推荐
回答by SLaks
Yes.
Just return arguments.callee;
是的。
只是return arguments.callee;
However, this is likely to result in very confusing code; I do not recommend it.
然而,这很可能会导致非常混乱的代码;我不推荐它。
回答by ataru
You can do what you want as following:
您可以执行以下操作:
// Do definition and execution at the same time.
var someFunction = (function someFunction() {
// do stuff
return someFunction
})();
console.log(someFunction)
arguments.calleeis not supported in JavaScript strict mode.
JavaScript 严格模式不支持arguments.callee。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
回答by ??? ?
Even sorter that all the above is:
甚至分拣机以上所有内容是:
f=()=>f
回答by zero_cool
There is a simple way to achieve this doing the following:
有一个简单的方法来实现这一点,请执行以下操作:
let intArr = [];
function mul(x){
if(!x){
return intArr.reduce((prev, curr) => prev * curr)
}
intArr.push(x);
return mul;
}
console.log(mul(2)(4)(2)()); => outputs 16
回答by konstantin555
It is also possible just to return the argument the self invokable function like
也可以只返回自调用函数的参数,如
console.log( (function(a) { return a; })(1) ); // returns 1