gpt4 book ai didi

javascript - 函数声明 vs 函数定义 JS

转载 作者:行者123 更新时间:2023-11-30 12:45:52 25 4
gpt4 key购买 nike

JavaScript 中的函数声明和函数定义有什么区别?我读了第 5.3.2 章 from this book

When nested, however, function declarations may only appear at the top level of the function they are nested within. That is, function definitions may not appear within if statements, while loops, or any other statements.

但不明白声明和定义之间的区别...请解释一下(注意:我理解函数表达式和函数声明之间的区别!)

最佳答案

重要的是要区分两种情况:

  • var f1 = function() {...}这通常称为函数表达式。

  • 函数 f2() {...}这通常称为函数声明。

无论你给它起什么名字,var f1 都会被提升到包装函数的顶部(但是 f1 只会在你写的那一行)。 但是 f2 的整个函数声明将被提升到包装函数的顶部。

有一篇很棒的帖子对此进行了更详细的解释 here .

关于javascript - 函数声明 vs 函数定义 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22504886/

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