admin管理员组文章数量:1334817
As gdoron pointed out,
var a = "a";
var b = "b";
a = [b][b = a,0];
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.
As gdoron pointed out,
var a = "a";
var b = "b";
a = [b][b = a,0];
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.
- 2 "a bit of hacky"??? a lot of hacky... :-) – gdoron Commented Mar 25, 2012 at 21:59
- Relevant: What's a better way to swap two argument values? – Šime Vidas Commented Mar 25, 2012 at 22:05
- Wouldn't it have made more sense to ment on godron's other answer and ask for clarification in your original question? – Felix Kling Commented Mar 25, 2012 at 22:36
- 1 @FelixKling. I suggested him to ask it in other question. ments are not the place of questions-answers. And it's not just clarification of the answer, it's totally different question (In my humble opinion!) – gdoron Commented Mar 25, 2012 at 23:00
2 Answers
Reset to default 9var 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 ma operator.
so finally it is a =[b][0]
- the first object in the [b]
array => b
assigned to a
Live DEMO
read @am not I am ments in this question:
When is the ma operator useful?
It's his code...
It might help (or hinder) to think of it terms of the semantically equivalent lambda construction (here, parameter c
takes the place of element 0):
a = (function(c) { b = a; return c; })(b);
本文标签: javascriptHow does bba0 swap between a and bStack Overflow
版权声明:本文标题:javascript - How does [b][b = a,0] swap between a and b? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742382294a2464334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论