JavaScript 的逗号操作符
除了分号之外,逗号允许你在同一个地方放多个语句。 例如:
for(var i=0, j=0; i<5; i++, j++, j++){
console.log("i:"+i+", j:"+j);
}
輸出:
i:0, j:0
i:1, j:2
i:2, j:4
i:3, j:6
i:4, j:8
当放一个表达式时,它由左到右计算每个表达式,并传回最右边的表达式。
例如:
function a(){console.log('a'); return 'a';}
function b(){console.log('b'); return 'b';}
function c(){console.log('c'); return 'c';}
var x = (a(), b(), c());
console.log(x); // 输出「c」
输出:
"a"
"b"
"c"
"c"
- 注意:逗号(
,
)操作符在 JavaScript 中所有的操作符里是最低的优先顺序,所以没有括号表达式将变为:(x = a()), b(), c();
。
实验
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW