错误:'let'声明只能在块内声明 [英] Error: 'let' declarations can only be declared inside a block
问题描述
在我的项目中,我有以下几行:
In my project I have these lines:
if (text.length > 0) {
let data = "Some value";
}
因为中只有一个语句,如果
,我想删除括号( {}
)。我的代码看起来像这样:
Because there is only one statement inside the if
, I wanted to remove the brackets ({ }
). My code then would have looked like this:
if (text.length > 0)
let data = "Some value";
但是我收到以下错误:
'let'声明只能在一个块内声明。
'let' declarations can only be declared inside a block.
<$> c $ c>如果在没有括号的情况下正常工作。为什么我会收到此错误?
Every other single statement inside if
works fine without the brackets. Why do I get this error?
推荐答案
let声明的变量的范围是定义它们的块,所以你需要括号来定义范围。
您可以使用let来建立仅限于单个表达式的变量:
Variables declared by let have as their scope the block in which they are defined, so you need the brackets to define the scope. You can use let to establish variables that are scoped only to a single expression:
var a = 5;
let(a = 6) console.log(a); // 6
console.log(a); // 5
注意:让表达式和让阻止是非标准的扩展程序,它们的支持已被删除。
Note: let expression and let block are Non-standard let extensions and their support has been dropped.
这篇关于错误:'let'声明只能在块内声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!