Javascript ternary w/ for loop error; "Uncaught SyntaxError: Unexpected token for"
Javascript ternary w/ for loop error; "Uncaught SyntaxError: Unexpected token for"
我不明白为什么我不能在三元运算中使用 for 循环。这是不起作用的代码:
this.ask = function() {
m = (isVoice) ? 'voice' : 'text';
switch (true) {
case m == 'voice' && typeof questions[timer.question].voice == 'string':
(++timer.attempts > timer.maxAttempts) ?
console.log('Stop'):
console.log('Play file (' + timer.attempts + '): ' + questions[timer.question].voice);
break;
case m == 'voice' && typeof questions[timer.question].voice == 'object':
(++timer.attempts > timer.maxAttempts) ?
console.log('Stop'):
for (i = 0; i < questions[timer.question].voice.length; i++) {
console.log(questions[timer.question].voice[i])
};
break;
default:
(++timer.attempts > timer.maxAttempts) ?
console.log('Stop'):
console.log('Say Text (' + timer.attempts + '): ' + questions[timer.question].text);
break;
}
};
特别是 m == 'voice' 和 typeof == 'object' 的情况抛出错误 "Uncaught SyntaxError: Unexpected token for"。如果我将那个案例更改为:
case m == 'voice' && typeof questions[timer.question].voice == 'object':
console.log('Audio, Array.');
if (++timer.attempts > timer.maxAttempts) {
console.log('Stop');
}
else {
for (i in questions[timer.question].voice) {
console.log(questions[timer.question].voice[i]);
}
}
break;
...然后一切正常。
这是为什么?
三元运算符的语法要求 "branches" 是表达式。您不能只是在那里放置任何任意语句;在 JavaScript 中,for
循环 不是 表达式。
您可以将循环包装在一个函数中并调用它,但只使用普通的 if
语句会简单得多。
如果将 for 循环括在括号中,它可能会起作用。
function() {
for (i = 0; i < questions[timer.question].voice.length; i++) {
console.log(questions[timer.question].voice[i])
}
}()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
但从风格上讲,您确实在挑战极限...
我不明白为什么我不能在三元运算中使用 for 循环。这是不起作用的代码:
this.ask = function() {
m = (isVoice) ? 'voice' : 'text';
switch (true) {
case m == 'voice' && typeof questions[timer.question].voice == 'string':
(++timer.attempts > timer.maxAttempts) ?
console.log('Stop'):
console.log('Play file (' + timer.attempts + '): ' + questions[timer.question].voice);
break;
case m == 'voice' && typeof questions[timer.question].voice == 'object':
(++timer.attempts > timer.maxAttempts) ?
console.log('Stop'):
for (i = 0; i < questions[timer.question].voice.length; i++) {
console.log(questions[timer.question].voice[i])
};
break;
default:
(++timer.attempts > timer.maxAttempts) ?
console.log('Stop'):
console.log('Say Text (' + timer.attempts + '): ' + questions[timer.question].text);
break;
}
};
特别是 m == 'voice' 和 typeof == 'object' 的情况抛出错误 "Uncaught SyntaxError: Unexpected token for"。如果我将那个案例更改为:
case m == 'voice' && typeof questions[timer.question].voice == 'object':
console.log('Audio, Array.');
if (++timer.attempts > timer.maxAttempts) {
console.log('Stop');
}
else {
for (i in questions[timer.question].voice) {
console.log(questions[timer.question].voice[i]);
}
}
break;
...然后一切正常。
这是为什么?
三元运算符的语法要求 "branches" 是表达式。您不能只是在那里放置任何任意语句;在 JavaScript 中,for
循环 不是 表达式。
您可以将循环包装在一个函数中并调用它,但只使用普通的 if
语句会简单得多。
如果将 for 循环括在括号中,它可能会起作用。
function() {
for (i = 0; i < questions[timer.question].voice.length; i++) {
console.log(questions[timer.question].voice[i])
}
}()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
但从风格上讲,您确实在挑战极限...