在条件表达式中声明变量(三元运算符)(declaring a variable within conditional expressions (ternary operator))

编程入门 行业动态 更新时间:2024-10-26 21:34:24
在条件表达式中声明变量(三元运算符)(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.

更多推荐

本文发布于:2023-08-04 15:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   变量   运算符   声明   条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!