小编Dom*_*rso的帖子

JavaScript Switch语句-可以使用案例值吗?

我想知道在执行代码块时是否有可能在Javascript switch语句中使用类似于“ this”的东西。

例如:

switch(x) {
    case 'The pyramids of Giza':
        console.log(this);
        break;
    case 'The Leaning Tower of Pisa':
        console.log(this);
        break;
    default:
        console.log('Not found');
}
Run Code Online (Sandbox Code Playgroud)

是相同的:

switch(x) {
    case 'The pyramids of Giza':
        console.log('The pyramids of Giza');
        break;
    case 'The Leaning Tower of Pisa':
        console.log('The Leaning Tower of Pisa');
        break;
    default:
        console.log('Not found');
}
Run Code Online (Sandbox Code Playgroud)

纯粹出于效率目的,谢谢!

javascript switch-statement

1
推荐指数
1
解决办法
978
查看次数

标签 统计

javascript ×1

switch-statement ×1