Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript 的逗号操作符 #57

Open
susouth opened this issue Dec 9, 2019 · 0 comments
Open

JavaScript 的逗号操作符 #57

susouth opened this issue Dec 9, 2019 · 0 comments
Labels
JavaScript 跟js相关的面试题 No.57

Comments

@susouth
Copy link
Contributor

susouth commented Dec 9, 2019

📚在线阅读:JavaScript 的逗号操作符 - No.57

除了分号之外,逗号允许你在同一个地方放多个语句。
例如:

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();
实验
JS Bin on jsbin.com<script src="https://static.jsbin.com/js/embed.min.js?3.39.11"></script>

扩展阅读:

@susouth susouth added JavaScript 跟js相关的面试题 No.57 labels Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript 跟js相关的面试题 No.57
Projects
None yet
Development

No branches or pull requests

1 participant