为什么我的JavaScript功能有时“未定义”? [英] Why is my JavaScript function sometimes "not defined"?

查看:119
本文介绍了为什么我的JavaScript功能有时“未定义”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我调用我的JavaScript函数。为什么有时 定义时会收到myFunction未定义的错误?

I call my JavaScript function. Why do I sometimes get the error 'myFunction is not defined' when it is defined?

例如。即使在这个例子中,我偶尔会得到'copyArray未定义':

For example. I'll occasionally get 'copyArray is not defined' even in this example:

function copyArray( pa ) {
    var la = [];
    for (var i=0; i < pa.length; i++)
        la.push( pa[i] );
    return la;
}

Function.prototype.bind = function( po ) {
    var __method = this;
    var __args = [];

    // Sometimes errors -- in practice I inline the function as a workaround.
    __args = copyArray( arguments );

    return function() {
        /* bind logic omitted for brevity */
    }
}

正如您所看到的,copyArray已经定义就在那里,因此这不能与脚本文件加载的顺序有关。

As you can see, copyArray is defined right there, so this can't be about the order in which script files load.

我一直在难以解决的情况下得到这个,其中调用函数位于另一个应该之后加载的文件中叫功能。但这是我可以呈现的最简单的情况,并且似乎是同样的问题。

I've been getting this in situations that are harder to work around, where the calling function is located in another file that should be loaded after the called function. But this was the simplest case I could present, and appears to be the same problem.

它不会100%发生,所以我怀疑某种与负载时序相关的问题。但我不知道是什么。

It doesn't happen 100% of the time, so I do suspect some kind of load-timing-related problem. But I have no idea what.

@Hojou:这是问题的一部分。我现在收到此错误的函数本身就是我的addLoadEvent,它基本上是公共库函数的标准版本。

@Hojou: That's part of the problem. The function in which I'm now getting this error is itself my addLoadEvent, which is basically a standard version of the common library function.

@James:我明白了,并且函数中没有语法错误。在这种情况下,也会报告语法错误。在这种情况下,我只得到'未定义'错误。

@James: I understand that, and there is no syntax error in the function. When that is the case, the syntax error is reported as well. In this case, I am getting only the 'not defined' error.

@David:这种情况下的脚本驻留在使用普通<引用的外部文件中; script src =file.js>< / script>页面头部的方法。

@David: The script in this case resides in an external file that is referenced using the normal <script src="file.js"></script> method in the page's head section.

@Douglas:有趣的想法,但如果是这种情况,我们怎么能永远呼叫用户 - 定义函数有信心吗?无论如何,我试过这个并且它没有用。

@Douglas: Interesting idea, but if this were the case, how could we ever call a user-defined function with confidence? In any event, I tried this and it didn't work.

@sk:这种技术已经在浏览器中测试过,基本上是从原型库。

@sk: This technique has been tested across browsers and is basically copied from the Prototype library.

推荐答案

<如果您只是在页面上包含脚本,则不可能发生这种情况。

It shouldn't be possible for this to happen if you're just including the scripts on the page.

当JavaScript代码开始执行时,copyArray函数应该始终可用,无论它是在它之前还是之后声明 - 除非您正在加载JavaScript文件动态地使用依赖库。如果是这种情况,会有各种各样的时间问题。

The "copyArray" function should always be available when the JavaScript code starts executing no matter if it is declared before or after it -- unless you're loading the JavaScript files in dynamically with a dependency library. There are all sorts of problems with timing if that's the case.

这篇关于为什么我的JavaScript功能有时“未定义”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆