JavaScript 函数可以返回自身吗?

发布于 2024-11-28 09:37:19 字数 365 浏览 2 评论 0原文

我可以编写一个返回自身的函数吗?

我正在阅读一些关于闭包的描述 - 参见示例 6 - 其中一个函数返回一个函数,所以您可以将 func()(); 作为有效的 JavaScript 进行调用。

所以我想知道函数是否可以以这样的方式返回自身,您可以将其无限期地链接到自身,如下所示:

func(arg)(other_arg)()(blah);

使用参数对象,被调用者或调用者?

Can I write a function that returns iteself?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月棠 2024-12-05 09:37:20

您可以执行以下操作:

// Do definition and execution at the same time.
var someFunction = (function someFunction() {

    // do stuff
    return someFunction
 })();

 console.log(someFunction)

参数。 JavaScript 严格模式不支持被调用者

https://developer.mozilla.org/en-US/docs /Web/JavaScript/Reference/Strict_mode

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.callee is not supported in JavaScript strict mode.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

娜些时光,永不杰束 2024-12-05 09:37:20

甚至以上所有的排序器都是:

f=()=>f

Even sorter that all the above is:

f=()=>f

淡笑忘祈一世凡恋 2024-12-05 09:37:20

有一个简单的方法可以实现这一点,执行以下操作:

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

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
耀眼的星火 2024-12-05 09:37:20

也可以只返回自调用函数的参数,例如

console.log( (function(a) {  return a; })(1) ); // returns 1

It is also possible just to return the argument the self invokable function like

console.log( (function(a) {  return a; })(1) ); // returns 1
尤怨 2024-12-05 09:37:19

有2-3种方法。正如您所说,一种是使用arguments.callee。如果您正在处理未存储分配给某处变量(您知道的)的匿名函数,这可能是唯一的方法:

(function() {
    return arguments.callee;
})()()()().... ;

第二种是使用函数的名称

function namedFunc() {
    return namedFunc;
}
namedFunc()()()().... ;

,最后一种是使用分配给的匿名函数一个变量,但你必须知道这个变量,所以在这种情况下,我看不出为什么你不能只给函数一个名称,并使用上面的方法

var storedFunc = function() {
    return storedFunc;
};
storedFunc()()()().... ;

它们在功能上都是相同的,但是 callee< /code> 是最简单的。

编辑:我同意 SLAks 的观点;我也无法推荐它

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):

(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.

Edit: And I agree with SLaks; I can't recommend it either

剑心龙吟 2024-12-05 09:37:19

是的。
只需return argument.callee;

但是,这可能会导致代码非常混乱;我不推荐它。

Yes.
Just return arguments.callee;

However, this is likely to result in very confusing code; I do not recommend it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文