无法找到Javascript函数

无法找到Javascript函数

问题描述:

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

I have the following code in document.ready()

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

    setMinContentHeight();

    function setMinContentHeight() {

        // removed for clarity
    }
}

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

I simply check if the page is correct (#site-master), then call my minimum height function, however I'm getting the following error in firebug: ReferenceError: setMinContentHeight is not defined.

我不是javascript专家,但这怎么可能?如果我将它移到document.ready()之外,该函数可以工作。我已经检查过if语句中的代码了。

I'm no javascript expert, but how can this be? The function works if I move it outside of document.ready(). I have checked and the code inside the if statement is reached.

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

Also, is this the best way of achieving what I want?

提前致谢。

从不中声明您的功能 for 语句:

function setMinContentHeight() {
    // removed for clarity
}

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

如果我们解决 ECMAScript规范 ,根据 第12章 如果条款被视为是声明(以及 ,而尝试/捕获等)。

If we address the ECMAScript specification, according to Chapter 12, if clause is considered to be a Statement (as well as for, while, with, try/catch, etc).

因此,遵循 Semantics 部分的

Hence, following the NOTE from Semantics section:


已知几种广泛使用的ECMAScript实现支持
使用 FunctionDeclaration 作为语句。然而,在
中的实现中存在
重要且不可调和的变化,这些语义应用于此类 FunctionDeclarations 。由于这些
不可调和的差异,使用 FunctionDeclaration 作为
语句会导致在
实现中无法可靠移植的代码。建议ECMAScript实现
不允许使用 FunctionDeclaration ,或者在遇到此类用法时发出警告
。 ECMAScript的未来版本可以
定义用于在
Statement 上下文中声明函数的替代可移植方法。

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.

这意味着我们无法保证在这种情况下的一致行为,因此,我们将始终在严格模式中获得异常如果函数是在语句中声明的。

It means that we cannot guarantee the consistent behavior in such cases, and, as a result, we will always get exception in strict mode in case if function was declared inside the statement.