If I use:
如果我使用:
1.09 * 1; // returns "1.09"
But if I use:
但如果我使用:
1,09 * 1; // returns "9"
I know that 1,09 isn't a number.
我知道1,09不是一个数字。
What does the comma do in the last piece of code?
逗号在最后一段代码中做了什么?
More Examples
if (0,9) alert("ok"); // alert
if (9,0) alert("ok"); // don't alert
alert(1); alert(2); alert(3); // 3 alerts
alert(1), alert(2), alert(3); // 3 alerts too
alert("2",
foo = function (param) {
alert(param)
},
foo('1')
)
foo('3'); // alerts 1, 2 and 3
5 个解决方案
#1
67
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.
逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
资料来源:https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5
evaluates to 5
. Obviously the comma operator is useful only for operations with side-effects.
例如,表达式1,2,3,4,5的计算结果为5.显然,逗号运算符仅对具有副作用的运算有用。
#2
5
Some more alerts to consider:
还需要考虑一些警报:
alert((0, 9));
alert((9, 0));
alert(("foo", "bar"));
Also, have a look at the Firebug console if you want to try this out interactively.
另外,如果您想以交互方式尝试,请查看Firebug控制台。
#3
4
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
看看这里 - 逗号代表多个表达式/语句。例如,在您的代码中,您可以使用如下所示的行:
var a=0, b=0, c=0;
This would declare all three variables without writing:
这将声明所有三个变量而不写:
var a=0;
var b=0;
var c=0;
Hope that helps.
希望有所帮助。
#4
2
The comma operator evaluates both of its operands (from left to right) and returns the value of thesecond
operand.
https://*.com/a/3561056/5934465
https://*.com/a/3561056/5934465
It should be like this!
它应该是这样的!
The comma operator evaluates each of its operands (from left to right) and returns the value of the
last
operand.逗号运算符计算每个操作数(从左到右)并返回最后一个操作数的值。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
#5
0
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
向对象添加/修改属性并将其返回到同一行是一种可能的用例:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger
property.
上面的匿名函数返回一个随机值大于输入值的对象,如果没有,则返回一个包含在更大属性中的数组中的输入值本身。
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
它仍然是语法糖(如箭头函数),但它确实缩短了行数......我想知道一些JS minifiers是否会以类似的方式自动检测和调整代码。在您的控制台中运行它:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)
#1
67
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.
逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。
Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
资料来源:https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator
For example, the expression 1,2,3,4,5
evaluates to 5
. Obviously the comma operator is useful only for operations with side-effects.
例如,表达式1,2,3,4,5的计算结果为5.显然,逗号运算符仅对具有副作用的运算有用。
#2
5
Some more alerts to consider:
还需要考虑一些警报:
alert((0, 9));
alert((9, 0));
alert(("foo", "bar"));
Also, have a look at the Firebug console if you want to try this out interactively.
另外,如果您想以交互方式尝试,请查看Firebug控制台。
#3
4
Have a look here - the comma stands for multiple expressions / statements. For example in your code you could use a line like this:
看看这里 - 逗号代表多个表达式/语句。例如,在您的代码中,您可以使用如下所示的行:
var a=0, b=0, c=0;
This would declare all three variables without writing:
这将声明所有三个变量而不写:
var a=0;
var b=0;
var c=0;
Hope that helps.
希望有所帮助。
#4
2
The comma operator evaluates both of its operands (from left to right) and returns the value of thesecond
operand.
https://*.com/a/3561056/5934465
https://*.com/a/3561056/5934465
It should be like this!
它应该是这样的!
The comma operator evaluates each of its operands (from left to right) and returns the value of the
last
operand.逗号运算符计算每个操作数(从左到右)并返回最后一个操作数的值。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator
#5
0
Adding/modifying properties to an object and returning it in the same line is a possible use-case:
向对象添加/修改属性并将其返回到同一行是一种可能的用例:
console.log(
((x) => (o = {biggerCond: r => r >= x},
o.r5 = Array.from(window.crypto.getRandomValues(new Uint16Array(5))),
o.isAnyBigger = o.r5.some(o.biggerCond),
o.bigger = o.isAnyBigger ? o.r5.filter(o.biggerCond) : [x], o )
)(5e4)
);
// Example
// {
// bigger: [58414, 56500, 63397],
// isAnyBigger: true,
// isBiggerCond: r => r >= x,
// r5: [58414, 12015, 56500, 63397, 43861]
// }
The above anonymous function returns an object with random values bigger than the input value or, if there's none, with the input value itself in an array in contained in the bigger
property.
上面的匿名函数返回一个随机值大于输入值的对象,如果没有,则返回一个包含在更大属性中的数组中的输入值本身。
It is still syntactic sugar (like arrow functions), but it does shorten the number of lines... I wonder if some JS minifiers detect and adjust the code in a similar way automatically. Run it in your console:
它仍然是语法糖(如箭头函数),但它确实缩短了行数......我想知道一些JS minifiers是否会以类似的方式自动检测和调整代码。在您的控制台中运行它:
((x)=>(o={biggerCond:r=>r>=x},o.r5=Array.from(window.crypto.getRandomValues(new Uint16Array(5))),o.isAnyBigger=o.r5.some(o.biggerCond),o.bigger=o.isAnyBigger?o.r5.filter(o.biggerCond):[x],o))(5e4)