我想知道在执行代码块时,是否可以在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');
}
等同于:
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');
}
这仅仅是为了提高效率而已,谢谢!
case
和break
之间的代码中,只需使用变量x
。更详细的解释即将呈现! - Noble Mushtakx
! - Domenic Corso