gpt4 book ai didi

javascript - 处理开关箱

转载 作者:行者123 更新时间:2023-12-04 01:54:00 24 4
gpt4 key购买 nike

如何使用switch语句执行以下操作:

String.prototype.startsWith = function( str ){
return ( this.indexOf( str ) === 0 );
}

switch( myVar ) {
case myVar.startsWith( 'product' ):
// do something
break;
}

这等效于:
if ( myVar.startsWith( 'product' )) {}

最佳答案

您可以做到,但这不是switch命令的逻辑使用:

String.prototype.startsWith = function( str ){
return ( this.indexOf( str ) === 0 );
};

var myVar = 'product 42';

switch (true) {
case myVar.startsWith( 'product' ):
alert(1); // do something
break;
}

关于javascript - 处理开关箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com