问题
This runs in ActionScript 3 and JavaScript. Why? I know how &&
and ||
work, but a list? Is this AS3 specific? Is this in other languages? I'm a mouth breathing PHP/AS2 programmer. Or did everyone already know this and I'm a tool for not reading documentation properly?
AS3
if (true, true, true) {
trace("true?")
}
//result - "true?" traced
JavaScript
if (true, true, true) {
alert("true?");
}
//result - "true?" alert message popped up
if (false, false, false) {
alert("true?");
}
else {
alert("false");
}
//result - "false" alert message popped up
if(true, false, false) {
alert("true?");
}
else {
alert("false");
}
//result - "false" alert message popped up
回答1:
I presume JavaScript has a comma operator like C, which takes multiple arguments and returns the last one. It's typically used to for loops where you want to initialize more than one value:
for(i=0, j=0; j< 10; j++) {
...
}
回答2:
The comma is used to evaluate expressions in a sequence, the same thing could be done with parenthesis groups separated by &&
来源:https://stackoverflow.com/questions/703896/as3-javascript-if-statement-commas-instead-of