[英]JavaScript: The `for loop` that works in an `if` statement does not work in a `ternary` statement? Why?
I've just picked up the concept of a ternary
statement, and tried to use it in place of an if / else if / else
statement that I'm familiar with. 我刚刚掌握了ternary
语句的概念,并试图用它代替我熟悉的if / else if / else
语句。 But the code seems to have a syntax error. 但是代码似乎有语法错误。 What is a ternary
statement unable to do that an if
statement can do? if
语句可以执行的ternary
语句不能执行的操作是什么? I wonder if ternary
statements are more appropriate for executing simple commands. 我想知道ternary
语句是否更适合执行简单命令。
The 'bin' array is a collection of sub-arrays, consisting of items discarded from the 'inventory' array, courtesy of the splice
and push
methods. “ bin”数组是子数组的集合,这些子数组由“库存”数组中丢弃的项目组成,这些对象是由splice
和push
方法提供的。
var bin = [ ['Orichalcum'], ['Rapier', 'Panacea'], ['Bow'], ['Antidote', 'Cheese', 'Whip'],
['Elixir', 'Herb'], ['Timbrel', 'Dagger', 'Boomerang'] ];
var message = (! bin.length) ?
'No items have been removed.'
: (bin.length === 1) ?
bin[0].length + ' items have been removed.'
: (
for (var i = 1; i < bin.length; i++) {
for (var j = 0; j < bin[i].length; j++) {
bin[0].push(bin[i][j]);
}
},
bin[0].length + ' items have been removed.'
);
alert(message);
The ? ? : operator is an expression, not a statement. :运算符是表达式,而不是语句。 This means you cannot nest other statements (such as for) inside it. 这意味着您不能在其中嵌套其他语句(例如for)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.