javascript - 在没有额外标志的情况下解决 JavaScript 缺少 goto 的问题

标签 javascript goto

在这种情况下,我如何模拟 goto 编程结构?

$.fn.hierarchy = function(info, ret) {
    if (info.constructor !== Object) {
        info = {children: info};
        goto label1; // Illegal JavaScript
    }

    if (!info.children) {
        info.children = [];
        goto label2; // Illegal JavaScript
    }

    label1:
    if (info.children.constructor !== Array)
        info.children = [info.children];

    label2:
    /*
    // Forget this code. It's irrelevant to my specific problem
    // (which is that JS doens't allow non-nested conditionals)
    //  and caused much confusion.
    if (!info.tagc)
        info.tagc = info.tag || 'div';
    */

我知道我可以将这些 goto 中的 ONE 作为 else 子句实现:

$.fn.hierarchy = function(info, ret) {
    if (info.constructor !== Object) {
        info = {children: info};
        //goto label1;
    }

    else if (!info.children) {
        info.children = [];
        goto label2; // Illegal JavaScript
    }

    //label1:
    if (info.children.constructor !== Array)
        info.children = [info.children];

    label2:
    /*
    // Forget this code. It's irrelevant to my specific problem
    // (which is that JS doens't allow non-nested conditionals)
    //  and caused much confusion.
    if (!info.tagc)
        info.tagc = info.tag || 'div';
    */

或者:

$.fn.hierarchy = function(info, ret) {
    if (info.constructor !== Object) {
        info = {children: info};
        goto label1; // Illegal JavaScript
    }

    if (!info.children) {
        info.children = [];
        //goto label2;
    }

    else {
        label1:
        if (info.children.constructor !== Array)
            info.children = [info.children];
    }

    //label2:
    /*
    // Forget this code. It's irrelevant to my specific problem
    // (which is that JS doens't allow non-nested conditionals)
    //  and caused much confusion.
    if (!info.tagc)
        info.tagc = info.tag || 'div';
    */

但我想要两者 goto。而且,不,我不想要额外的标志。


编辑:

@Luis Espinal:您提出的解决方案不起作用。如果 info{children: 'a'},您的程序无法将 info.children 转换为 [a].

$.fn.hierarchy = function(info, ret) {
    if (info.constructor !== Object) {
        info = {children: info};
        // goto label1; // Illegal JavaScript
        // label1:
        if (info.children.constructor !== Array){
            info.children = [info.children];
        }
    }
    else if (!info.children) {
        info.children = [];
        // goto label2; // Illegal JavaScript
        // label2:
        /*
        // Wrong. This has to be placed outside all of this.
        if (!info.tagc)
        {
            info.tagc = info.tag || 'div';
        }
        */
    }
    /* the following code is missing:

    else {
        // Handles the case when info.constructor === Object
        // from the beginning
        // AND
        // info.children isn't an array
        if (info.children.constructor !== Array)
            info.children = [info.children];
    }
    */

编辑:你们中的一些人似乎认为第四个条件与我的问题有关。问题实际上是我无法执行以下操作:

If condition1 Then action1
If !condition1 && condition2 Then action2
If !condition2 && condition3 && regardlessOf(condition1) Then action3

不使用标志(临时 bool 变量)。

基本上,如果 condition1 为真,我不必测试 condition2,如果 condition2 为真,我不需要不必测试 condition3。但是,如果 condition1 && !condition2,我可能必须测试 condition3

最佳答案

也许将标签更改为函数,然后转到 setTimeout(functionname, 0)

例如,代替:

label1:
    // do something
    goto label2

label2:
    // do something
    goto label1

尝试这样的事情:

function label1 () {
    // do something
    setTimeout(label2, 0);
    return; // may be needed if not at the end of function
}

function label2 () {
    // do something
    setTimeout(label1, 0);
    return; // may be needed if not at the end of function
}

(你必须使用超时,因为第一个 JavaScript 还没有优化尾调用,第二个是因为你不想阻塞浏览器,0 的超时会将你的回调放在事件循环的末尾)

关于javascript - 在没有额外标志的情况下解决 JavaScript 缺少 goto 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5146563/

相关文章:

javascript - 尝试在 angular2 中使用 stomp.js

c++ - 错误 C2059 : syntax error: '}' C++

excel - VBA 嵌套错误 GoTo

c++ - 在变量定义之前转到 - 它的值会发生什么?

javascript - Vue.js : passing item as parameter to computed prop in v-for, 返回已排序的子数组?

javascript - Viewport.el.mask根本不出现在javascript中,太快了?

javascript - ES6 Map 仅返回对象键数组

javascript - 我如何使这个 Javascript/jQuery 动画更好?

c - 在 C 中使用 goto 进行错误处理的奇怪行为

c - C标准中对可变修改类型的switch语句约束的解释