JavaScript 中逗号运算符的作用是什么?

发布于 2024-09-15 20:01:41 字数 559 浏览 5 评论 0原文

如果我使用:

1.09 * 1; // returns "1.09"

但如果我使用:

1,09 * 1; // returns "9"

我知道 1,09 不是一个数字。

最后一段代码中的逗号有什么作用?

更多示例

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紅太極 2024-09-22 20:01:41

逗号运算符计算两者
它的操作数(从左到右)和
返回第二个值
操作数。

来源 https://developer.mozilla。 org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator

例如,表达式 1,2,3,4,5 的计算结果为 5。显然,逗号运算符仅对有副作用的操作有用。

console.log(1,2,3,4,5);
console.log((1,2,3,4,5));

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

For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.

console.log(1,2,3,4,5);
console.log((1,2,3,4,5));

耀眼的星火 2024-09-22 20:01:41

还有一些需要考虑的:

console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));

Some more to consider:

console.log((0, 9));
console.log((9, 0));
console.log(("foo", "bar"));

只为守护你 2024-09-22 20:01:41

特定的语法允许面包店功能性地烘烤面包,将其交给顾客食用,并将返回的数量保持为零。所有这些都利用括号内的箭头函数。

(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))

.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.

The specific syntax allows a bakery to functionally bake bread, hand it to the customer for consumption and also keep the number of returns to zero. All these by utilizing parenthesized arrow functions.

(new Array(3)).fill()
.map(()=>({state:"dough", bake(){this.state="baked"}, consume(){this.state="consumed"}}))

.map(bread=>(console.log(`Adding ${bread.state} to oven.`), bread.bake(), bread))
.map(bread=>(console.log(`Consuming ${bread.state} bread.`), bread.consume(), bread))
.map(bread=>console.log(`Bread is now ${bread.state}.`))
Adding dough to oven.
Adding dough to oven.
Adding dough to oven.
Consuming baked bread.
Consuming baked bread.
Consuming baked bread.
Bread is now consumed.
Bread is now consumed.
Bread is now consumed.
把梦留给海 2024-09-22 20:01:41

向对象添加/修改属性并在同一行中返回它是一种可能的用例:

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]
// }

上面的匿名函数返回一个对象,其随机值大于输入值,或者,如果没有,则输入值本身位于 bigger 属性中包含的数组中。

它仍然是语法糖(例如 箭头函数),但它确实缩短了行数...我想知道是否有一些 JS 压缩器会自动以类似的方式检测和调整代码。在控制台中运行它:

((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)

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:

((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)
成熟的代价 2024-09-22 20:01:41

看看这里 - 逗号代表多个表达式 /声明。例如,在您的代码中,您可以使用如下行:

var a=0, b=0, c=0;

这将声明所有三个变量,而无需编写:

var a=0;
var b=0;
var c=0;

希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文