Top*_*era 81 javascript comma-operator
如果我使用:
1.09 * 1; // returns "1.09"
Run Code Online (Sandbox Code Playgroud)
但如果我使用:
1,09 * 1; // returns "9"
Run Code Online (Sandbox Code Playgroud)
我知道1,09不是一个数字.
逗号在最后一段代码中做了什么?
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
Run Code Online (Sandbox Code Playgroud)
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
Run Code Online (Sandbox Code Playgroud)
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
Run Code Online (Sandbox Code Playgroud)
Yuv*_*dam 84
逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值.
资料来源: https ://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
例如,表达式1,2,3,4,5
求值为5
.显然,逗号运算符仅对具有副作用的操作有用.
还需要考虑一些警报:
console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));
Run Code Online (Sandbox Code Playgroud)
另外,如果您想以交互方式尝试,请查看Firebug控制台.
归档时间: |
|
查看次数: |
9504 次 |
最近记录: |