私信  •  关注

Joe K

Joe K 最近创建的主题
Joe K 最近回复了
12 年前
回复了 Joe K 创建的主题 » jquery开关盒插件?

首先,Switter语句只在int上工作。我不知道JavaScript为什么会从C/C++中保留这个限制,但是它在那里。

使用嵌套的if-then-else块(而不是开关)可能会导致代码高度不可读,如果您处理的选项不止一小部分。

但是,就像C和C++一样,也有一些解决方法,这一点涉及到使用GOTO这样的“断裂”,这并不总是邪恶的。这是一种情况(大量嵌套,如果是else的话),goto将使代码更加高效和可读。在C/C++中,你将使用Goto来实现这一点,标签位于IF系列的末端(现在是交换机支架的末端),跳过跳转到交换机上下文。

switch (1) { //yes, that is a hardcoded 1, we only want the switch block for the break keyword
    if (yourString == "Case 1") {
        do_stuff_1();
        break; // well, we're done, let's get out of here.
    }

//implicit else - if you matched the first one, you'd be out of here

    if (yourString == "Case 2") {
        do_stuff_2();
        break; // well, we're done, let's get out of here.
    }

    // etc.....

    default:
        do_error_condition();
} //end of switch