开发者

Javascript switch statement fails

Probably a really silly problem, but: this prints 'yeeeeeees' in the console, but in the switch statement the Unknown step error is thrown.

What am I doing wrong?

        if (step === 'menuLoaded') console.log('yeeeeeeeees');
        switch (step) {
            case 'menuLoaded':
                this.window.activate_side_menu();
     开发者_如何学运维           this.handleDomainFtp();
                break;
            case 'ftpStep1':
                this.handleDomainFtp(1);
                break;
            case 'ftpStep2':
                this.handleDomainFtp(2);
                break;
            case 'startMysql':
                alert('starting mysql');
                this.handleDomainMysqlList();
                break;
            case 'mysqlList':
                this.handleDomainMysql();
                break;
            case 'mysqlPage':
                this.handleDomainMysql(true);
                break;
            case 'done':
                run();
                break;
            default:
                throw new Error('Unknown step: ' + step);
                break;
        }


The error I'm getting is TypeError: Object [object DOMWindow] has no method 'activate_side_menu' - have you made sure to declare that function somewhere in the available scope?


Since there apparently is no other way of closing my question I'll just answer it myself.

The problem was with the following code. Later I have another switch statement with exactly the same Error() thrown.

Like I said, probably something silly....

But thanks for everyones time!


Most likely needed to assign 'this' to 'var self' and use 'self' in place of this.


try using the default statement without the "break;"

default:
            throw new Error('Unknown step: ' + step);
}


I'm rather sure JavaScript's switch() won't accept strings - you have to use integer values. If you'd like to use names, use some enum like variables:

const step_menuLoaded = 0;
const step_menuFtp1 = 1;
const step_menuFtp2 = 2;
const step_startSql = 3;
...

Have you checked your code/source for non-printable characters inside the strings then?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜