简体   繁体   English

切换案例的Javascript范围变量?

[英]Javascript scope variable to switch case?

In C you can scope a variable to a switch case, like this . 在C中,您可以将变量范围限定为开关案例,如下所示

With javascript I get unexpected token using the following: 使用javascript,我使用以下方法得到意外的令牌:

 const i = 1 switch (i) { // variables scoped to switch var s var x = 2342 case 0: s = 1 + x break case 1: s = 'b' break } 

Is there another way to do this or should I just declare my variables outside the switch? 有没有其他方法可以做到这一点,还是我应该在交换机外声明我的变量?

EDIT: 编辑:

This is a workaround I considered but it didn't end up working. 这是我考虑过的一种解决方法,但它并没有最终起作用。 The reason being that each case has its own scope. 原因是每个案例都有自己的范围。

 const i = 1 switch (i) { case i: // variables scoped to switch var s var x = 2342 case 0: s = 1 + x break case 1: s = 'b' break } 

some alternative: 一些替代品:

/* curly braces inside the case */

const i = 1

switch (i) {
  case 0: {
    let x = 2342;
    let s = 1 + x;
    console.log(x+' & '+s+' from inside');
  } break;
  case 1: {
    let x = 2342;
    let s = 'b';
    console.log(x+' & '+s+' from inside'); /* 2342 & b from inside */
  } break;
}

console.log(x+' & '+s+' from outside'); /* Uncaught ReferenceError */

or 要么

/* curly braces outside the switch */

const i = 1

{
  let x = 2342;
  let s;
  switch (i) {
    case 0:
      s = 1 + x;
      break;
    case 1:
      s = 'b';
      break;
  }
  console.log(x+' & '+s+' from inside'); /* 2342 & b from inside */
}

console.log(x+' & '+s+' from outside'); /* Uncaught ReferenceError */

Since var creates variables at function scope anyway, using it is pretty pointless. 因为var无论如何都会在函数范围创建变量,因此使用它是毫无意义的。 For this to work at a granularity below function scopes you'll have to use let and a browser/compiler which supports it, and then introduce a new block which you can scope things to (within switch it's simply invalid syntax): 为了能够在函数范围以下的粒度下工作,你必须使用let和支持它的浏览器/编译器,然后引入一个可以将事物范围限定的新块(在switch它只是无效的语法):

if (true) {
    let s;

    switch (i) {
        ...
    }
}

This scopes s to the if block, which for all intents and purposes is identical to the " switch scope" here. 这个范围s if块,它的所有意图和目的都与这里的“ switch scope”相同。

If you cannot support let , you'll need to use an IIFE: 如果你不能支持let ,你需要使用IIFE:

(function () {
    var s;

    switch (...) ...
})();

No, this is invalid syntax. 不,这是无效的语法。 A case or default statement is expected within a switch . switch预期casedefault语句。 You can check the specification here: http://www.ecma-international.org/ecma-262/5.1/#sec-12.11 您可以在此处查看规范: http//www.ecma-international.org/ecma-262/5.1/#sec-12.11

You can also try entering your code in a JSLinter and see that this an error: http://jslint.com/ 您也可以尝试在JSLinter中输入代码,看到这是一个错误: http ://jslint.com/

Expected 'case' and instead saw 'var'.

The workaround that you're considering would be the same thing as putting them outside the switch statement. 您正在考虑的解决方法与将它们放在switch语句之外是一回事。 Remember, var has function -level scope, not block-level scope. 请记住, var具有function -level范围, 而不是块级范围。 That means they are bound to the entire function containing the switch. 这意味着它们绑定到包含开关的整个函数。 You should declare them outside of the switch because that is where they are accessible. 您应该在交换机之外声明它们,因为这是可以访问的地方。

const i = 1;
var s;
var x = 2342;

switch (i) {
  case 0:
    s = 1 + x;
    break;
  case 1:
    s = 'b';
    break;
  default:
    break;
}

console.log("s is " + s);

It should be declared outside the switch. 它应该在交换机外声明。 The below might help: 以下可能会有所帮助:

var i = 1, x = 2342;
var s;
switch (i)
 {        
    case 0:
      s = 1 + x;
      break;
    case 1:
      s = 'b';
      break;
}
console.log("s is " + s);

JavaScript defines 3 levels of scope: JavaScript定义了3个范围:

  • Global - Anything not delcared in a function 全局 - 任何未在函数中进行任何操作的东西
  • Function - Anything declared in a function using the var keyword 函数 - 使用var关键字在函数中声明的任何内容
  • Block - Anything declared in a block container ( {} ) using let Block - 使用let在块容器( {} )中声明的任何内容

So, to creae a scope an entire construct, you have two choices: Function or Block 因此,要为整个构造创建一个范围,您有两个选择:Function或Block

In order to get the the behavior you are looking for with a function: 为了获得您正在寻找的功能:

 const i = 1 function doSwitch(data){ // variables are not scoped to switch, but are // scoped to the function, which only contains // the switch. var s; var x = 2342; switch (data) { case 0: s = 1 + x; break; case 1: s = 'b'; break; default: s = "other"; } console.log("s is " + s) } doSwitch(18); 

Or, in order to get the functionality with a block using let 或者,为了使用let获取块的功能

 const i = 1; // Wrapping the switch in its own block allows for let to work: { // variables are not scoped to switch, but are // scoped to the function, which only contains // the switch. let s; let x = 2342; switch (i) { case 0: s = 1 + x; break; case 1: s = 'b'; break; default: s = "other"; } console.log("s is " + s) } // Test: console.log(s, x); // ReferenceError 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM