有多种条件时如何使用CONST [英] How to use CONST when there are multiple conditions

查看:283
本文介绍了有多种条件时如何使用CONST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看到的问题是下面的情况 - 怎么会这样?我在逻辑树中有超过2个叉的情况下使用const。与const类似的模式是什么?

  function getResult(input){
let result;

switch(input):{
case(1):{result = x;}
case(2):{result = y;}
case 3):{result = x;}
... etc
}

//...附加条件和函数取决于switch语句的结果

}

谢谢,

解决方案

  const result = [x,y,x] [input  -  1]; 

这是一个通过索引访问的数组。


so I'm getting on the const train and want to start avoiding let at all costs.

the problem I'm seeing is the below case - how would I use const in a situation with more than 2 forks in a logic tree. what's the equivalent pattern with const?

function getResult(input) {
    let result;

    switch (input): {
        case (1): { result=x;}
        case (2): { result=y;}
        case (3): { result=x;}
        ...etc
    }

//...additional conditionals and functions depending on the outcomes of switch statement

}

thanks,

解决方案

 const result = [x,y,x][input - 1];

It's an array that's accessed by index.

这篇关于有多种条件时如何使用CONST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆