gpt4 book ai didi

javascript - 大括号 {} 内的函数调用

转载 作者:行者123 更新时间:2023-11-28 12:12:11 24 4
gpt4 key购买 nike

我正在尝试使用主要提供商之一来实现在线聊天。他们建议我嵌入如下代码:

<script type="text/javascript">
function embedScriptInsideHTMLtags() {
// responsible code
}
{
embedScriptInsideHTMLtags();
}
</script>

我的问题是,上面和下面有什么区别:

<script type="text/javascript">
function embedScriptInsideHTMLtags() {
// responsible code
}
embedScriptInsideHTMLtags();
</script>

最佳答案

第一个示例使用所谓的 block statement 。在这种情况下,两个示例没有区别。根据 MDN 链接:

Variables declared with var or created by function declarations in non-strict mode do not have block scope. Variables introduced within a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope.

但是在严格模式

identifiers declared with let and const do have block scope

In strict mode, starting with ES2015, functions inside blocks are scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode.

关于javascript - 大括号 {} 内的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59358378/

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