在条件表达式中声明变量(三元运算符)(declaring a variable within conditional expressions (ternary operator))
是否可以在条件表达式中声明变量?
例如:下面的代码返回语法错误(因为我在条件表达式中声明了变量x?)。
var a = document.getElementById("userData"); var d = a.value; function() { (d.length>15)?( alert("your input was too long")):( var x = parseInt(d).toString(2), a.value=x ); }显然,这可以通过简单地添加var x; 在声明之外,但是可以在这里声明变量吗?
Is it possible to declare the variable within a conditional expression?
for example: The code below return a syntax error (because I've declared the variable x within the conditional expression?).
var a = document.getElementById("userData"); var d = a.value; function() { (d.length>15)?( alert("your input was too long")):( var x = parseInt(d).toString(2), a.value=x ); }obviously this can be fixed by simply adding var x; outside the statement, but is it possible for variables to be declared here?
最满意答案
是否可以在条件表达式中声明变量?
编号var是一个语句, 条件表达式的操作数是表达式。 语言语法不允许它。 值得庆幸的。
Is it possible to declare the variable within a conditional expression?
No. var is a statement, and the operands to a conditional expression are expressions. The language grammar doesn't allow it. Thankfully.
更多推荐
发布评论