简单的javascript函数定义问题

发布于 2024-10-11 01:28:16 字数 551 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁微风不噪 2024-10-18 01:28:16

更新

您更新后的代码与原始代码有很大不同。问题似乎是您传递给 setTimeout 的字符串(这让我感到惊讶,但很容易复制)。我会更改

function First () {
    setTimeout("Second()", 50)
};

function First () {
    setTimeout(Second, 50);
}

...或者如果您需要将参数传递给 Second:(

function First() {
    setTimeout(function() {
        Second(param0, param1);
    }, 50);
}

请注意,函数声明末尾不需要 ;,但需要一个在 setTimeout 不会出错之后[你实际上并不需要它,在这种情况下,可怕的“分号插入”会为你插入它,但是。 ..].)

上面的第二个和第三个版本使用函数引用。您的原始版本使用一个字符串,然后进行编译,这是不必要的,并且似乎是问题所在(如 此示例使用字符串失败,但是这个带有函数参考的作品)。

原始答案

截至下面的答案,您的问题中引用的代码是:

function First() {Second();};
function Second() {First();};

该代码可以正常工作。这是一个无限循环(嗯,不是无限循环,因为最终实现不会有更多的堆栈空间用于返回地址),但直到它因此而崩溃之前,它都会正常工作。 示例

如果您的实际代码看起来更像这样,它将失败:

var First = function() {
    Second();
};
First();
var Second = function() {
    First();
};

...因为这是非常不同的,它使用函数表达式(作为分步代码的一部分进行处理)而不是函数声明(在进入作用域时进行处理,在任何分步代码之前),并且在定义 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 change

function First () {
    setTimeout("Second()", 50)
};

to

function First () {
    setTimeout(Second, 50);
}

...or if you need to pass parameters to Second:

function First() {
    setTimeout(function() {
        Second(param0, param1);
    }, 50);
}

(Note that there's no need for a ; at the end of a function declaration, but one after the setTimeout 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:

function First() {Second();};
function Second() {First();};

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:

var First = function() {
    Second();
};
First();
var Second = function() {
    First();
};

...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 before Second is defined. See this other answer here on StackOverflow for more detail on the distinction between a function expression and a function declaration.

弥枳 2024-10-18 01:28:16

好的,我想我看到了你的问题。我敢打赌你的代码包装在函数内,对吗?那么就不会有函数 Second 之类的东西了。

这行不通:

(function() {
    function First () {
        setTimeout("Second()", 50)
    }
    function Second () {
        alert('hi!');
    }
    First();
})();

但这行得通:

(function() {
    function First () {
        setTimeout(Second, 50)
    }
    function Second () {
        alert('hi!');
    }
    First();
})();

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:

(function() {
    function First () {
        setTimeout("Second()", 50)
    }
    function Second () {
        alert('hi!');
    }
    First();
})();

But this will work:

(function() {
    function First () {
        setTimeout(Second, 50)
    }
    function Second () {
        alert('hi!');
    }
    First();
})();
荒人说梦 2024-10-18 01:28:16

我刚刚尝试了你的代码并调用了“Second();”第一的。它在 Chrome 中运行良好。
当然它会永远循环。

在 Javascript 中,变量在调用函数时很晚才绑定。全局对象也只是另一个很晚才“绑定”的变量。一切都可以随时(异步)更改,这就是为什么一个函数不能要求另一个函数可用的原因。
“缺失”的函数可能只是在调用该函数之前由某种其他机制添加。仅在执行函数之前,JS 运行时才应检查该函数在作用域中是否可用。

这就是它在 Chrome 中工作的原因。在 Javascript 中,你实际上在做类似这样的事情:

var GLOB = this; // bind global obj to variable

GLOB["First"] = function() {
   GLOB["Second"]();
};

GLOB["Second"] = function() {
   GLOB["First"]();
};

调用 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:

var GLOB = this; // bind global obj to variable

GLOB["First"] = function() {
   GLOB["Second"]();
};

GLOB["Second"] = function() {
   GLOB["First"]();
};

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 as function funcname(){}, but might not be detected as error by your "broken" JS-interpreter.

I hope this helps,
Juve

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文