简单的javascript函数定义问题
function First () {
setTimeout("Second()", 50)
};
function Second () { //I'm very confident this conditional works fine
if (document.getElementsByClassName("l")[0].href ==
document.getElementById("myFrame").getAttribute("src"))
{
First();
}
else
{
var newLink = document.getElementsByClassName("l")[0].href; //
document.getElementById("myFrame").setAttribute("src", newLink);
}};
First ();
问题是,当定义 First() 时,我收到 Second 未定义的错误。如何解决这个问题?
function First () {
setTimeout("Second()", 50)
};
function Second () { //I'm very confident this conditional works fine
if (document.getElementsByClassName("l")[0].href ==
document.getElementById("myFrame").getAttribute("src"))
{
First();
}
else
{
var newLink = document.getElementsByClassName("l")[0].href; //
document.getElementById("myFrame").setAttribute("src", newLink);
}};
First ();
The problem is that when First() is defined, I get the error that Second isn't defined. How can this be solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新
您更新后的代码与原始代码有很大不同。问题似乎是您传递给 setTimeout 的字符串(这让我感到惊讶,但很容易复制)。我会更改
为
...或者如果您需要将参数传递给
Second
:(请注意,函数声明末尾不需要
;
,但需要一个在setTimeout
不会出错之后[你实际上并不需要它,在这种情况下,可怕的“分号插入”会为你插入它,但是。 ..].)上面的第二个和第三个版本使用函数引用。您的原始版本使用一个字符串,然后进行编译,这是不必要的,并且似乎是问题所在(如 此示例使用字符串失败,但是这个带有函数参考的作品)。
原始答案
截至下面的答案,您的问题中引用的代码是:
该代码可以正常工作。这是一个无限循环(嗯,不是无限循环,因为最终实现不会有更多的堆栈空间用于返回地址),但直到它因此而崩溃之前,它都会正常工作。 示例
如果您的实际代码看起来更像这样,它将失败:
...因为这是非常不同的,它使用函数表达式(作为分步代码的一部分进行处理)而不是函数声明(在进入作用域时进行处理,在任何分步代码之前),并且在定义
Second
之前调用First
。请参阅 StackOverflow 上的其他答案,了解有关两者之间区别的更多详细信息函数表达式和函数声明。Update
Your updated code is quite different from your original code. The problem appears to be the string you're passing to
setTimeout
(which surprised me, but was easily replicated). I would changeto
...or if you need to pass parameters to
Second
:(Note that there's no need for a
;
at the end of a function declaration, but one after thesetTimeout
wouldn't go amiss [you don't actually need it, the horror that is "semicolon insertion" will insert it for you in this case, but...].)The second and third versions above use a function reference. Your original uses a string which is then compiled, which is unnecessary and appears to be the problem (as this example using the string fails, but this one with the function reference works).
Original Answer
As of the answer below, the code quoted in your question was:
That code will work just fine. It's an infinite loop (well, not infinite, because eventually the implementation won't have any more stack space for return addresses), but until it blows up because of that it'll work fine. Example
It will fail if your actual code looks more like this:
...because that's very different, it uses function expressions (which are processed as part of the step-by-step code) rather than function declarations (which are processed upon entry to the scope, before any step-by-step code) and it has a call to
First
beforeSecond
is defined. See this other answer here on StackOverflow for more detail on the distinction between a function expression and a function declaration.好的,我想我看到了你的问题。我敢打赌你的代码包装在函数内,对吗?那么就不会有函数 Second 之类的东西了。
这行不通:
但这行得通:
Ok, I think I see your problem. I bet your code wrapped inside of a function, right? Then there would be no such thing as function Second.
This will not work:
But this will work:
我刚刚尝试了你的代码并调用了“Second();”第一的。它在 Chrome 中运行良好。
当然它会永远循环。
在 Javascript 中,变量在调用函数时很晚才绑定。全局对象也只是另一个很晚才“绑定”的变量。一切都可以随时(异步)更改,这就是为什么一个函数不能要求另一个函数可用的原因。
“缺失”的函数可能只是在调用该函数之前由某种其他机制添加。仅在执行函数之前,JS 运行时才应检查该函数在作用域中是否可用。
这就是它在 Chrome 中工作的原因。在 Javascript 中,你实际上在做类似这样的事情:
调用 GLOB["Second"](); 就像 Chrome 中的魅力一样(当然它会循环)。也许您的浏览器/JS-implementation/dev-tool 对于函数定义有更多限制,并且让您在定义函数之前不要使用它们。
然后,您可以使用此
obj["funcname"] = function() {}
语法,其作用与function funcname(){}
相同,但可能不会被检测到作为你的“损坏的”JS 解释器的错误。我希望这有帮助,
尤文
I just tried your code and called "Second();" first. It worked fine in Chrome.
It will loop forever of course.
In Javascript variables are bound very late at invocation of a function. The global object is also just another variable that is also "bound" very late. Everything can change at any time (asynchronously) that's why a function must not require that another function is available.
The "missing" function might just be added by some other mechanism just before the function is invoked. Only just before a function is executed, the JS-runtime should check if this functions is available in the scope.
That's why it works in Chrome. In Javascript you are actually doing something like this:
Invoking
GLOB["Second"]();
works like a charm in Chrome (and it loops of course). maybe your browser/JS-implementation/dev-tool is more restrictive regarding function definitions, and let's you not use functions before they are defined.Then you might use this
obj["funcname"] = function() {}
syntax, which does the same asfunction funcname(){}
, but might not be detected as error by your "broken" JS-interpreter.I hope this helps,
Juve