2016-12-06 117 views
1

在C可以範圍的變量的開關的情況下,像thisJavaScript範圍變量切換大小寫?

用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 
 
}

是否有另一種方式做到這一點,或者我應該只需要聲明的開關超出了我的變量?

編輯:

這是一種解決方法我認爲,但最終沒有工作。原因是每個案件都有自己的範圍。

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 
 
}

+1

你'之開關語句無效至少在'JS'中。您可以在案例或外部開關內定義變量。 – Ionut

+1

因爲顯然你想在'switch'之外訪問's',你爲什麼想把它的範圍限制在'switch'語句中...... ?! – deceze

+0

@deceze這只是一個例子,有很多原因需要這樣做。 –

回答

2

由於var功能範圍創建變量無論如何,使用它是很沒有意義的。對於這個在下面的功能範圍粒度工作,你將不得不使用let和瀏覽器/編譯器支持它,然後引入新的塊,你可以範圍的事情(內switch它只是無效的語法):

if (true) { 
    let s; 

    switch (i) { 
     ... 
    } 
} 

這作用域sif塊,這對於所有的意圖和目的是相同的「switch範圍」在這裏。

如果不能支持let,你需要使用一個IIFE:

(function() { 
    var s; 

    switch (...) ... 
})(); 
+1

第一個例子根本不需要'if'。只需將代碼包裝在'{}'中。 –

+0

我不確定哪些情況可能會被解釋爲對象字面量,所以我建議安全的if語句。當然,你可以嘗試沒有。 – deceze

+3

我已經試過了。這是完全合法的。正如你的'if'的分支不會被混淆爲一個對象字面量,圍繞整個塊也不會。 –

-1

的JavaScript定義了3個級別的作用域:

  • 全球 - 在一個功能不delcared任何
  • 功能 - 任何在函數中調用聲明var關鍵字
  • - 在一個塊容器({})宣佈使用let

所以,CREAE一個範圍的整個構建什麼,你有兩個選擇:功能或模塊

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

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

+1

你不能在開關中間聲明它是什麼問題 –

+1

@DarrenSweeney是的,謝謝。一開始沒有聽到。答案已被修改。 –

+0

新答案看起來不錯+1 thx –

0

應該開關之外聲明。以下可能有所幫助:

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); 
2

不,這是無效的語法。一個casedefault語句是switch內的預期。您可以點擊這裏規格:http://www.ecma-international.org/ecma-262/5.1/#sec-12.11

您也可以嘗試在JSLinter輸入您的代碼,看到這樣的錯誤:http://jslint.com/

Expected 'case' and instead saw 'var'. 

,你正在考慮的解決方法是一樣的東西把它們放在switch語句之外。請記住,var具有功能 - 電平範圍,塊級範圍。這意味着它們綁定到包含開關的整個功能。您應該在交換機之外聲明它們,因爲這是它們可訪問的位置。

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); 
12

一些替代:

/* 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 */ 

/* 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 */