在Switch Case语句中出现重复的Const声明错误

时间:2021-06-26 10:21:43

I have the following code and I get the error 'Duplicate Declaration query_url'.

我有以下代码,我收到错误'Duplicate Declaration query_url'。

  switch(condition) {
    case 'complex':
      const query_url = `something`;
      break;
    default:
      const query_url = `something`;
      break;
  }

I understand that query_url is getting declared twice which isn't right. But i don't know how to resolve this. Can someone please help on what should be the correct way to make this work?

我知道query_url被声明了两次,这是不对的。但我不知道如何解决这个问题。有人可以帮助解决这项工作的正确方法吗?

5 个解决方案

#1


5  

if query_url can have multiple values depending on the switch branch obviously you need a variable ( declare either with var or let ).

如果query_url可以有多个值,具体取决于switch分支,显然你需要一个变量(用var或let声明)。

const is set once and stays that way.

const设置一次并保持这种状态。

example usage with let

let的示例用法

let query_url = '';
switch(condition) {
  case 'complex':
    query_url = `something`;
    break;
  default:
    query_url = `something`;
    break;
}

#2


193  

Try wrapping the cases in blocks:

尝试用块包装案例:

switch(condition) {
  case 'complex': {
    const query_url = `something`;
    … // do something
    break;
  }
  default: {
    const query_url = `something`;
    … // do something else
    break;
  }
}

#3


8  

I personally prefer (and tend to abuse) the following in these sorts of cases:

在这些情况下,我个人更喜欢(并且倾向于滥用)以下内容:

const query_url = (()=>
{
     switch(condition)
           case 'complex': return 'something';
           default       : return 'something-else';
})();

(this requires ES6 or declaring "use-strict" in Node 4.x though)

(这需要ES6或在Node 4.x中声明“use-strict”)

Update: Alternatively, much more compact depending on if there is any logic there or if it's a simple assignment:

更新:或者,更紧凑,取决于是否有任何逻辑或如果它是一个简单的任务:

const query_url = {complex : 'something'}[condition] || 'something-else';

Also, of course, depends on the amount of outside-logic embedded in those switch statements!

当然,还取决于那些switch语句中嵌入的外部逻辑的数量!

#4


1  

Just put your switch in a function with some return statements :

只需将您的开关放在带有一些return语句的函数中:

var condition;
function aSwitch(condition){
switch(condition) {
    case 'complex':
      return 'something';
    default:
      return 'something';
  }
}
const query_url = aSwitch(condition);

#5


0  

const query_url={
  complex:'something complex',
  other:'other thing'
}[condition]

The drawback is,you can't have default with object,you need to have addition check of condition.

缺点是,你不能默认使用对象,你需要对条件进行额外检查。

#1


5  

if query_url can have multiple values depending on the switch branch obviously you need a variable ( declare either with var or let ).

如果query_url可以有多个值,具体取决于switch分支,显然你需要一个变量(用var或let声明)。

const is set once and stays that way.

const设置一次并保持这种状态。

example usage with let

let的示例用法

let query_url = '';
switch(condition) {
  case 'complex':
    query_url = `something`;
    break;
  default:
    query_url = `something`;
    break;
}

#2


193  

Try wrapping the cases in blocks:

尝试用块包装案例:

switch(condition) {
  case 'complex': {
    const query_url = `something`;
    … // do something
    break;
  }
  default: {
    const query_url = `something`;
    … // do something else
    break;
  }
}

#3


8  

I personally prefer (and tend to abuse) the following in these sorts of cases:

在这些情况下,我个人更喜欢(并且倾向于滥用)以下内容:

const query_url = (()=>
{
     switch(condition)
           case 'complex': return 'something';
           default       : return 'something-else';
})();

(this requires ES6 or declaring "use-strict" in Node 4.x though)

(这需要ES6或在Node 4.x中声明“use-strict”)

Update: Alternatively, much more compact depending on if there is any logic there or if it's a simple assignment:

更新:或者,更紧凑,取决于是否有任何逻辑或如果它是一个简单的任务:

const query_url = {complex : 'something'}[condition] || 'something-else';

Also, of course, depends on the amount of outside-logic embedded in those switch statements!

当然,还取决于那些switch语句中嵌入的外部逻辑的数量!

#4


1  

Just put your switch in a function with some return statements :

只需将您的开关放在带有一些return语句的函数中:

var condition;
function aSwitch(condition){
switch(condition) {
    case 'complex':
      return 'something';
    default:
      return 'something';
  }
}
const query_url = aSwitch(condition);

#5


0  

const query_url={
  complex:'something complex',
  other:'other thing'
}[condition]

The drawback is,you can't have default with object,you need to have addition check of condition.

缺点是,你不能默认使用对象,你需要对条件进行额外检查。