切换案例

时间:2018-03-03 10:37:06

标签: javascript node.js

我在运行代码时难以检查切换案例中的两个条件,因为此代码打印'未知',因为在javascript中

['police',false]!=['police',false]

有没有办法使用switch - case而不是嵌套if来实现此代码?

var option='police';
var urgent=false;

switch([option,urgent])
{
case ['police',true]:
       console.log('police,true');
       call_police_urgent();
       break;
case ['police',false]:
       console.log('police,false');
       call_police();
       break;
case ['hospital',true]:
       console.log('hospital,true');
       call_hospital_urgent();
       break;
case ['hospital',false]:
       console.log('hospital,false');
       call_hospital();
       break;
case ['firestation',true]:
       console.log('firestation,true');
       call_firestation_urgent();
       break;
case ['firestation',false]:
       console.log('firestation,false');
       call_firestation();
       break;
default:
       console.log('unknown');
}

2 个答案:

答案 0 :(得分:0)

我不知道你要做什么,但是你上面的代码javascript引擎和运行时环境会对你大喊大叫。

其次[]字面值可以且永远不会等于另一个[]字面值

在这两者之间选择

var option='police';
var urgent=false;

function first_switch(option,urgent) {
    switch(option) {
    case "police":
        if ( urgent )
            console.log('police,true');
        else
            console.log('police,false');
        break;
    case "hospital":
        if ( urgent )
            console.log('hospital,true');
        else
            console.log('hospital,false');
        break;
    case "firestation":
        if ( urgent )
            console.log('firestation,true');
        else
            console.log('firestation,false');
        break;
    default:
        console.log('unknown');
    }
}

function second_switch(option,urgent) {

    if ( urgent ) {

        switch(option) {
        case "police":
        case "hospital":
        case "firestation":
            console.log(`${option}`, "true");
            break;
        default:
            console.log('unknown');
        }

        return ;
    }

    switch(option) {
    case "police":
    case "hospital":
    case "firestation":
        console.log(`${option}`, "false");
        break;
    default:
        console.log('unknown');
    }
}

first_switch(option,urgent);
first_switch(option, true);

second_switch(option, urgent);
second_switch(option, true);

答案 1 :(得分:0)

我们可以在不使用开关案例的情况下创建相同的功能。我们可以创建一个查找表,如:

var emergencyLookupTable = {
  police: [{
      case: true,
      fn: call_police_urgent
    },
    {
      case: false,
      fn: call_police
    }
  ],
  hospital: [{
      case: true,
      fn: call_hospital_urgent
    },
    {
      case: false,
      fn: call_firestation_urgent
    }
  ],
  firestation: [{
      case: true,
      fn: call_firestation_urgent
    },
    {
      case: false,
      fn: call_firestation
    }
  ]
}

并将此对象传递给正在寻找正确案例的emergency

function emergency(lookup, option, urgent) {
  if (lookup[option]) {
    lookup[option]
      .filter(function(obj) {
        return obj.case === urgent
      })
      .forEach(function(obj) {
        obj.fn()
      })
  } else {
    console.log('unknown')
  }
}

emergency(emergencyLookupTable, 'police', true)

工作示例

var emergencyLookupTable = {
  police: [{
      case: true,
      fn: call_police_urgent
    },
    {
      case: true,
      fn: call_police_urgent2
    },
    {
      case: false,
      fn: call_police
    }
  ],
  hospital: [],
  firestation: []
}

function emergency(lookup, option, urgent) {
  if (lookup[option]) {
    lookup[option]
      .filter(function(obj) {
        return obj.case === urgent
      })
      .forEach(function(obj) {
        obj.fn()
      })
  } else {
    console.log('unknown')
  }
}

function call_police_urgent() {
  console.log('call the police!')
}

function call_police_urgent2() {
  console.log('call the police again!')
}

function call_police() {
  console.log('call the police..')
}

emergency(emergencyLookupTable, 'police', true)
emergency(emergencyLookupTable, 'police', false)

相关问题