AS3/JavaScript if statement commas instead of & &

家住魔仙堡 提交于 2019-12-23 15:15:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!