gpt4 book ai didi

javascript - 函数声明不能​​嵌套在非函数 block 中

转载 作者:数据小太阳 更新时间:2023-10-29 06:10:58 26 4
gpt4 key购买 nike

我正在阅读有关函数声明与函数表达式的内容,但我无法弄清楚以下语句的含义:

Function Declarations occur as standalone constructs and cannot be nested within non-function blocks.

请有人用一个例子来解释作者的意思,准确地说:“......不能嵌套在非功能 block 中”。

链接是:https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

最佳答案

我不知道作者的意思是这在物理上是不可能的,或者不应该做更多的事情。根据我的理解,作者的意思是:

var y = true;
if (y) {
function example() {
alert('hi');
return true;
}
}

这里的函数是在条件语句中声明的,这很好,因为 x 为真,但如果它为假,函数将永远不会被声明,当我们想要调用 example 函数什么都不会发生,因为它从未被声明过。所以应该是

function example() {
"use strict";
return true;
}
var y = true;
if (y) {
example();
}

在上面的代码中,如果满足条件,我们仍然会调用 example 函数,但是由于 example 是在条件语句之外定义的,所以我们可以使用它而不管条件语句如何。 This Post has more information about it .希望这就是你的意思

关于javascript - 函数声明不能​​嵌套在非函数 block 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930932/

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