gpt4 book ai didi

javascript - JavaScript 中的 `x = y, z` 逗号赋值

转载 作者:行者123 更新时间:2023-12-03 03:09:18 26 4
gpt4 key购买 nike

Possible Duplicate:
Javascript syntax: what comma means?

我在阅读 this article 时发现了这段代码(使用 Ctrl+F 搜索 Andre Breton ):

//function returning array of `umbrella` fibonacci numbers
function Colette(umbrella) {
var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon;
Array.prototype.embrace = [].push;

while(2 + staircase++ < umbrella) {
bassoon = galleons + brigantines;
armada.embrace(brigantines = (galleons = brigantines, bassoon));
}

return armada;
}

x = (y = x, z) 是什么意思?构建的意思是?或者更具体地说,y = x, z 的作用是什么?意思是?我将其称为逗号赋值,因为它看起来像赋值并且有一个逗号。

In Python ,这意味着元组解包(或在本例中为打包)。这里也是同样的情况吗?

最佳答案

这是逗号运算符。

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

The resultant value when a,b,c,...,n is evaluated will always be the value of the rightmost expression, however all expressions in the chain are still evaluated (from left to right).

<小时/>

因此,在您的情况下,仍会评估分配,但最终值将是巴松管

结果:

galleons = brigantines
brigantines = bassoon
armada.embrace(basson)
<小时/>

更多信息:Javascript "tuple" notation: what is its point?

关于javascript - JavaScript 中的 `x = y, z` 逗号赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com