[b] [b = a,0]如何在a和b之间交换? [英] How does [b][b = a,0] swap between a and b?

查看:132
本文介绍了[b] [b = a,0]如何在a和b之间交换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gdoron 指出,

var a = "a";
var b = "b";

a = [b][b = a,0];

交换 a b ,虽然看起来有点 hacky ,但它引起了我的好奇心,我对它的运作方式非常好奇。这对我没有任何意义。

Will swap a and b, and although it looks a bit of hacky, it has triggered my curiosity and I am very curious at how it works. It doesn't make any sense to me.

推荐答案

var a = "a";
var b = "b";

a = [b][b = a, 0];

让我们将最后一行分成几部分:

Let's break the last line into pieces:

[b]       // Puts b in an array - a safe place for the swap.
[b = a]   // Assign a in b
[b = a,0] // Assign a in b and return the later expression - 0 with the comma operator.

所以最后它是 a = [b] [0] - 分配给 [b] array => b 中的第一个对象> a

so finally it is a =[b][0] - the first object in the [b] array => b assigned to a

现场演示

阅读@am不是我在这个问题上的评论:

逗号运算符何时有用?

这是他的代码......

read @am not I am comments in this question:
When is the comma operator useful?
It's his code...

这篇关于[b] [b = a,0]如何在a和b之间交换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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