JavaScript中的`x = y,z`逗号分配 [英] `x = y, z` comma assignment in JavaScript

查看:131
本文介绍了JavaScript中的`x = y,z`逗号分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript语法:逗号是什么意思?

我遇到过阅读本文时的代码(执行 Ctrl + F 搜索 Andre Breton ):

I came across the code while reading this article (do a Ctrl+F search for 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 是什么意思?我称之为逗号分配,因为它看起来像作业并且有逗号。

What does the x = (y = x, z) construct mean? Or more specifically, what does the y = x, z mean? I'm calling it comma assignment because it looks like assignment and has a comma.

在Python中,它意味着元组解包(或在这种情况下打包)。这是同样的情况吗?

In Python, it meant tuple unpacking (or packing in this case). Is it the same case here?

推荐答案

这是逗号运算符。


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

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

当评估a,b,c,...,n时的结果值将始终是最右边表达式的
值,但是仍然评估
链中的所有表达式(从左到右)右边)。

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






所以在你的情况下,仍然会评估分配,但是最终价值为 bassoon

结果:

galleons = brigantines
brigantines = bassoon
armada.embrace(basson)






更多信息: Javascript" tuple"符号:它有什么意义?

这篇关于JavaScript中的`x = y,z`逗号分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆