JavaScript switch 语句中的 Firebug 调试令人困惑

发布于 2024-12-19 20:27:24 字数 790 浏览 4 评论 0原文

我在JavaScript中有以下switch语句:(

switch(scrollable.direction){
  case "top" :
    break;
  case "left" :
    break;
  case "right" :
    scrollable.select("."+lineCssClass).invoke("setStyle", {float: "right"});
    break;
  case "bottom" :
    alert("Bottom scrolling not implemented yet ! Sorry !");
    return;
}

“调用”位是prototype.js,但它与问题无关)

它位于函数内部。我希望如果该值为 "bottom" 则显示一条消息并且方法执行停止。

问题是,如果值为 "top",则会执行 break,但执行会跳转到 return; 语句,而不是退出 switch 语句。

实际上,我通过在返回后添加一个额外的 break; 解决了这个问题,这实际上是死代码,因为它永远无法执行。

但我很想知道为什么它首先执行“返回”?

编辑:抱歉,“返回”实际上并未执行。我正在使用 Firebug 逐步执行代码,它实际上踩到并突出显示了“return”行,但它没有被执行。

我的代码中还有其他问题导致它无法按预期工作,我错误地归咎于此。

I have the following switch statement in JavaScript :

switch(scrollable.direction){
  case "top" :
    break;
  case "left" :
    break;
  case "right" :
    scrollable.select("."+lineCssClass).invoke("setStyle", {float: "right"});
    break;
  case "bottom" :
    alert("Bottom scrolling not implemented yet ! Sorry !");
    return;
}

(the "invoke" bit is prototype.js, but it's no relevant to the question anyway)

It is inside a function. I want that if the value is "bottom" a message is displayed and the method execution stops.

The problem is that if the value is e.g. "top", the break is executed, but the execution jumps to the return; statement instead of exiting the switch statement.

I actually solved the problem by adding an additionnal break; after the return, which is actually dead code since it can never be executed.

But I would be curious to know why it executed the "return" in the first place ?

Edit: I am sorry, the "return" wasn't actually executed. I was stepping through the code using Firebug and it actually stepped on and highlighted the "return" line, but it wasn't executed.

There are other problems in my code that cause it not to work as expected, and I was wrongly blaming this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

杯别 2024-12-26 20:27:24

我敢打赌,您的 scrollable.direction 给您的磨损值不是真正的方向。

在你的 switch 语句中添加一个 default 子句(顺便说一句,你应该总是这样做)来检查它

switch(scrollable.direction){
  /*...*/
  case "bottom" :
    alert("Bottom scrolling not implemented yet ! Sorry !");
    return;
  default:
    console.log(scrollable.direction, 'is not a direction');
    break;
}

My bet is that your scrollable.direction is giving you a worng value that is not a real direction.

Add a default clause to your switch statement (as you should always do, btw) to check it out

switch(scrollable.direction){
  /*...*/
  case "bottom" :
    alert("Bottom scrolling not implemented yet ! Sorry !");
    return;
  default:
    console.log(scrollable.direction, 'is not a direction');
    break;
}
黒涩兲箜 2024-12-26 20:27:24

正如我在对问题所做的编辑中所说,问题实际上不在于返回;而在于返回。被执行了,但是 Firebug 给人的印象是当我一步步执行 switch 语句时。

另外,经过一些更多的测试,只有当我将 switch/case 语句放在 try 块中时,我才能重现这个奇怪的事情。

正如 Missingno 建议的那样,我将留下这个问题,以防其他人对此感到困惑Firebug 的行为。

在此处输入图像描述

As I said in the edit I made to the question, the problem was actually not that the return; was executed, but that Firebug gave the impression it was when I went step by step through the switch statement.

Alsot, after some more tests, I can reproduce this weird thing only when I enclose the switch/case statement in a try block.

As missingno suggested I will leave this question in case other people are confused by this behaviour of Firebug.

enter image description here

雪花飘飘的天空 2024-12-26 20:27:24

我在使用 Firebug 但在 IF 语句上时也出现了类似的问题。

if(multiship == true) 
{
// do something
} 
else 
{
// do something
}

调试器显示脚本执行采用 if 路径,而不是让 multiship 变量等于 false。

Similar sort of issue occurred with me while using Firebug but on the IF statement.

if(multiship == true) 
{
// do something
} 
else 
{
// do something
}

The debugger showed that the script execution took the if path instead of having multiship variable equal to false.

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