简体   繁体   English

Javascript:关于Fibonacci函数中Return()()格式的尾部递归

[英]Javascript: About Return()() format tail recursion in Fibonacci function

function fibonacci(n) {
    return (function(a, b, i) {
        return (i < n) ? arguments.callee(b, a + b, i + 1) : a;
    })(1, 1, 1);
}

Hey, I am a rookie, I could understand the math logical part of this function, what I did not understand is the parameter passing progress, it passes (1,1,1) to the function(abi), which seems not very normal. 嘿,我是菜鸟,我可以理解此函数的数学逻辑部分,我不明白的是参数传递进度,它将(1,1,1)传递给了函数(abi),这似乎不是很正常。

function fibonacci(n) {
    return (function(a, b, i) {
        return (i < n) ? arguments.callee(b, a + b, i + 1) : a;
    })
    regular function pass a parameter like the way function(1 1 1)
}

I can not understand this, I tried to abstract it into finally the 我无法理解,我尝试将其抽象为

function fibonacci(n){
    return()()
}

format, but how could this pass the parameter, I guess it is something about the return call could not handle a function call after it which also works in it. 格式,但是如何传递参数,我想这是关于返回调用无法在它也起作用之后处理函数调用的问题。

So, is there some basic stuff I havent learnt? 那么,有没有我学过的基本知识? Like some default format like the entries in MDN? 像某些默认格式一样,例如MDN中的条目?

Thnx a lot. 非常感谢。

 return (function(a, b, i) { return (i < n) ? arguments.callee(b, a + b, i + 1) : a; })(1, 1, 1); 

is an anonymous function expression that is immediately invoked. 是立即被调用的匿名函数表达式。 The parenthesis around it are not required . 不需要用括号括起来。

The usage of arguments.callee is deprecated, this would better have been written with a named function expression: 不推荐使用arguments.callee的用法,最好使用命名函数表达式编写:

return (function recurse(a, b, i) {
    return (i < n) ? recurse(b, a + b, i + 1) : a;
})(1, 1, 1);

which is equivalent to the function declaration and invocation 这等效于函数的声明和调用

function recurse(a, b, i) {
    return (i < n) ? recurse(b, a + b, i + 1) : a;
}
return recurse(1, 1, 1);

Your final abstraction is wrong. 您的最终抽象是错误的。 It's not return()() but it is: 它不是return()()而是:

return      ( function(){} )()

Note that the () does not belong to return since return is not a function. 注意()不属于return因为return不是函数。 In this case, () act as grouping parenthesis. 在这种情况下, ()充当分组括号。 In the same way when you use it like: 以相同的方式使用时,如下所示:

return (1+1)*2

Here, adding *2 behind the () works because the parenthesis is not part of a function invocation but is instead used as a grouping operator. 在这里,在()后面添加*2可行的,因为括号不是函数调用的一部分,而是用作分组运算符。 Or more precisely, an expression evaluator. 或更准确地说,是一个表达式评估器。

You see, how (1+1)*2 works is that the () is recognized as not being part of a function call, thus it's contents are treated as an expression. 您会看到(1+1)*2工作原理是()被识别为不是函数调用的一部分,因此将其内容视为表达式。 So it is parsed as: 因此它被解析为:

temporary_variable = 1+1;
temporary_variable * 2;

In the same way, (function(){})() is parsed as: 同样, (function(){})()解析为:

temporary_variable = function(){};
temporary_variable();

Thus, the whole thing is basically doing this: 因此,整个过程基本上就是这样:

function f (n) {
    temporary_variable = function (a,b,i) {
        /* ... */
    }
    return temporary_variable(1,1,1);
}

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

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