1
我很高興JavaScript有新的let statement來聲明一個塊作用域局部變量。但是,在switch語句中聲明let
變量時,似乎會出現類型錯誤,這對於使用let
來說非常有用。爲什麼switch語句只有一個底層塊?
function test(x) {
'use strict';
switch (x) {
case 0:
let foo;
break;
case 1:
let foo; // TypeError for redeclaration.
break;
}
}
果然,MDN顯示了這個例子:
您可能會遇到switch語句中的錯誤,因爲只有一個底層塊。
那麼爲什麼switch語句只有一個底層塊呢?
你不能在同一個塊中有2個'foo'。 –
@JohnnyMopp我不確定你讀了我的問題。 – styfle
http://stackoverflow.com/questions/2524397/what-is-the-javascript-variable-scope-in-a-switch-case-statment –