0) { setMinContentHeight(); functi-6ren">
gpt4 book ai didi

找不到 Javascript 函数

转载 作者:行者123 更新时间:2023-11-30 06:57:20 25 4
gpt4 key购买 nike

我在 document.ready() 中有以下代码

if ($("#site-master").length > 0) {

setMinContentHeight();

function setMinContentHeight() {

// removed for clarity
}
}

我只是检查页面是否正确 (#site-master),然后调用我的最小高度函数,但是我在 firebug 中收到以下错误:ReferenceError: setMinContentHeight is not defined。

我不是 JavaScript 专家,但这怎么可能呢?如果我将它移到 document.ready() 之外,该函数就可以工作。我检查了一下,if语句里面的代码到了。

此外,这是实现我想要的目标的最佳方式吗?

提前致谢。

最佳答案

永远不要iffor 语句中声明您的函数:

function setMinContentHeight() {
// removed for clarity
}

if ($("#site-master").length > 0) {
setMinContentHeight();
}

如果我们解决 ECMAScript specification , 根据 Chapter 12 , if 子句被认为是一个语句(以及forwhilewithtry/catch 等)。

因此,遵循语义部分的注意:

Several widely used implementations of ECMAScript are known to support the use of FunctionDeclaration as a Statement. However there are significant and irreconcilable variations among the implementations in the semantics applied to such FunctionDeclarations. Because of these irreconcilable differences, the use of a FunctionDeclaration as a Statement results in code that is not reliably portable among implementations. It is recommended that ECMAScript implementations either disallow this usage of FunctionDeclaration or issue a warning when such a usage is encountered. Future editions of ECMAScript may define alternative portable means for declaring functions in a Statement context.

这意味着我们不能保证在这种情况下的行为一致,因此,如果在 语句.

关于找不到 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573202/

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