JavaScript切换语句 - 可能使用大小写值?

时间:2015-08-22 13:15:31

标签: javascript switch-statement

我想知道在执行代码块时是否可以在Javascript switch语句中使用类似'this'的东西。

例如:

switch(x) {
    case 'The pyramids of Giza':
        console.log(this);
        break;
    case 'The Leaning Tower of Pisa':
        console.log(this);
        break;
    default:
        console.log('Not found');
}

与:

相同
switch(x) {
    case 'The pyramids of Giza':
        console.log('The pyramids of Giza');
        break;
    case 'The Leaning Tower of Pisa':
        console.log('The Leaning Tower of Pisa');
        break;
    default:
        console.log('Not found');
}

这纯粹是出于提高效率的目的,谢谢!

3 个答案:

答案 0 :(得分:4)

您可以访问$('.nav-tabss').on('click','a',function(){ var branding = $(this).text(); if ( branding === "Branding" ) { $('.branding').show(); $('.adverise').hide(); $('.print').hide() } 语句中正在测试的变量;毕竟,如果switch等于“吉萨金字塔”,则x也必须是您希望在x内使用的值。

case

答案 1 :(得分:2)

当您访问$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $source); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // if https curl_setopt($ch, CURLOPT_SSLVERSION, 3); // or set this option curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch); $error = curl_error($ch); $file = $data = curl_exec($ch); curl_close($ch); if (file_exists($file)) { header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3"); header('Content-Disposition: attachment; filename="sometrack.mp3"'); header("Content-Transfer-Encoding: binary"); header('Content-length: ' . filesize($file)); header('X-Pad: avoid browser bug'); header('Cache-Control: no-cache'); readfile($file); } else { echo "no file"; } 时,case:中指定的值的值始终是放在case:语句中的变量。这是因为只有当变量等于switch中指定的值时才会访问case:,因此您知道如果您访问过该情况,那么变量的值必须与case:中指定的值相同。因此,这是您正在寻找的代码:

case:

答案 2 :(得分:1)

//Create some users
var user1 = {name: 'modulus admin', age: 42, roles: ['admin', 'moderator', 'user']};
var user2 = {name: 'modulus user', age: 22, roles: ['user']};
var user3 = {name: 'modulus super admin', age: 92, roles: ['super-admin', 'admin', 'moderator', 'user']};

// Insert some users
collection.insert([user1, user2, user3], function (err, result) {
  if (err) {
    console.log(err);
  } else {
    console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.length, result);
  }
  //Close connection
  db.close();
});

应该做的伎俩