[英]How can function reference itself within itself in JavaScript?
I understand recursion in terms of how code is executed and why you might need it.我根据代码的执行方式以及您可能需要它的原因来理解递归。 What I am wondering about is that is it possible that function can reference itself within itself?
我想知道的是,function 是否可以在自身内部引用自身?
Given the following example:给定以下示例:
function factorial(num) {
if(num ===0) {
return 1
}
return (num * factorial(num - 1));
}
factorial(2)
I want to understand what is happening under the hood in terms of how variables are stored in memory and how they're called and why is it possible to reference factorial
inside a factorial
function.我想了解变量如何存储在 memory 中以及如何调用它们以及为什么可以在
factorial
function 中引用factorial
的幕后发生的事情。
The way I understand how it is going to be executed at the moment:我理解目前将如何执行的方式:
factorial
on the stack that will reference an object on the heap.factorial
,它将引用堆上的 object。 At this moment factorial
still points to nowherefactorial
依然无处可去factorial(2)
which will take the reference on the stack where factorial
points to, find the function on the heap and call it.factorial(2)
它将获取factorial
指向的堆栈上的引用,在堆上找到 function 并调用它。 What I don't understand is that how when factorial
is called, will it know what is factorial
and where to find it?我不明白的是,当调用
factorial
时,它会知道什么是factorial
以及在哪里可以找到它? Is it related to closures somehow?它是否与闭包有关?
Another example(jest)另一个例子(笑话)
const someFunction = jest.fn((value) => {
expect(someFunction).toHaveBeenCalled()
})
Why I can reference someFunction
inside the someFunction
, as mentioned I suspect it is related to memory and how variables are stored, but I don't grasp the concept fully.,为什么我可以在
someFunction
中引用someFunction
,如前所述,我怀疑它与 memory 以及变量的存储方式有关,但我没有完全掌握这个概念。,
By using a named function declaration/ expression , the call checks the inner scope of the function and if it is not inside, it check the name of the called function and then the outer scope. By using a named function declaration/ expression , the call checks the inner scope of the function and if it is not inside, it check the name of the called function and then the outer scope.
However, a name can be provided with a function expression.
但是,可以使用 function 表达式提供名称。 Providing a name allows the function to refer to itself, and also makes it easier to identify the function in a debugger's stack traces
提供名称允许 function 引用自身,并且还可以更容易地在调试器的堆栈跟踪中识别 function
By using arrow functions , the name of function is not set and does not remain by renaming as opposite of a named function declaration, where the name is always callable from the inner scope.通过使用箭头函数,function 的名称未设置,并且不会通过重命名为命名 function 声明的对面而保留,其中名称始终可以从内部 Z31A1FD140BE4BEF2D511E121EC9A 调用。
The Function Guide of the MDN Web Docs contains the information you seek. MDN Web 文档的 Function 指南包含您寻求的信息。 There are multiple ways you can reference a function from inside its body.
您可以通过多种方式从 function 内部引用它。 It boils down to the fact, that
归结为一个事实,即
a function can access all variables and functions defined inside the scope in which it is defined
a function 可以访问定义在 scope 中的所有变量和函数
You can read this as a function can access everything defined in the scope in which it was defined.您可以将其阅读为 function 可以访问定义它的 scope 中定义的所有内容。 This includes itself.
这包括它自己。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.