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

查看:41
本文介绍了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元组";符号:它的意义是什么?

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

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