gpt4 book ai didi

javascript - 命名函数(而不是匿名)和创建对它的引用有什么区别?

转载 作者:行者123 更新时间:2023-12-02 14:49:17 26 4
gpt4 key购买 nike

在我发起的另一个帖子的评论中,有人这样说:

@adlwalrus yes. try this: var foo = function bar(){}; console.log(foo); But be aware that bar is only function name (what does it mean I'm not sure exactly myself) and not a reference to it, so you can't call it by doing bar(). And assigning (even named function) is not the same as declaring a function. Hoisting (bumping to top of the scope) only works for declarations, assignment will stay in place. – valentinas 6 hours ago

如果无法使用 bar() 调用函数名称,它有什么作用?

最佳答案

让函数调用自身。

var x = function y(val){
if (val){
console.log(val);
y(val-1);
}
};
x(5);
> 3
> 2
> 1
y(3);
> ReferenceError: y is not defined

关于javascript - 命名函数(而不是匿名)和创建对它的引用有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12292514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com