处理开关盒

发布于 2024-11-29 10:38:33 字数 327 浏览 0 评论 0原文

我怎样才能用 switch 语句做这样的事情:

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

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

这相当于:

if ( myVar.startsWith( 'product' )) {}

How can I do something like this with a switch statement:

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

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

This is the equivalent of:

if ( myVar.startsWith( 'product' )) {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小伙你站住 2024-12-06 10:38:33

可以做到这一点,但这不是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;
}

You can do it, but it's not a logical use of the switch command:

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;
}
坏尐絯 2024-12-06 10:38:33

最好的方法是添加三元运算符,尝试一下它应该可以完美工作

let myVar = 'product 42';
switch (myVar) {
   case myVar.startsWith('product') ? myVar : false :
       alert(1); // do something
       break;
 }

Best way to do this by adding ternary operator, Try this it should work perfectly

let myVar = 'product 42';
switch (myVar) {
   case myVar.startsWith('product') ? myVar : false :
       alert(1); // do something
       break;
 }
美人如玉 2024-12-06 10:38:33

像这样:-

var x="product";

switch({"product":1, "help":2}[x]){
case 1:alert("product");
    break;
 case 2:alert("Help");
    break;
};

Like this:-

var x="product";

switch({"product":1, "help":2}[x]){
case 1:alert("product");
    break;
 case 2:alert("Help");
    break;
};
成熟的代价 2024-12-06 10:38:33

你可以这样做这个

BEGINNING = 0;
MIDDLE = 1;
END = 2;
NO_WHERE = -1;

String.prototype.positionOfString = function(str) {
    var idx = this.indexOf(str);

    if (idx === 0) return BEGINNING;
    if (idx > 0 && idx + str.length === this.length) return END;
    if (idx > 0) return MIDDLE;
    else return NO_WHERE;
};

var myVar = ' product';

switch (myVar.positionOfString('product')) {
case BEGINNING:
    alert('beginning'); // do something
    break;
case MIDDLE:
    alert('middle');
    break;
case END:
    alert('END');
    break;
default:
    alert('nope');
    break;
}

You could do something like this:

BEGINNING = 0;
MIDDLE = 1;
END = 2;
NO_WHERE = -1;

String.prototype.positionOfString = function(str) {
    var idx = this.indexOf(str);

    if (idx === 0) return BEGINNING;
    if (idx > 0 && idx + str.length === this.length) return END;
    if (idx > 0) return MIDDLE;
    else return NO_WHERE;
};

var myVar = ' product';

switch (myVar.positionOfString('product')) {
case BEGINNING:
    alert('beginning'); // do something
    break;
case MIDDLE:
    alert('middle');
    break;
case END:
    alert('END');
    break;
default:
    alert('nope');
    break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文