javascript变量使用数组交换 [英] javascript variable swapping using arrays
问题描述
[a,b] = [b,a]
上面的代码是如何交换a和b的?
How does the above code swap a and b ?
我可以看到表达式的右侧包装了一个数组,但是我无法理解它是怎么回事被分配到左侧。
I could see that the Right hand side of the expression crates an array, but I could not understand how it can be assigned to the left hand side.
请看这个小提琴 http://jsfiddle.net/sethunath/MqWKH/
更新:仅适用于firefox!
Update : Works only in firefox !
推荐答案
JavaScript 1.7添加解构分配,你在那里观察。少数浏览器支持JavaScript 1.7。但值得注意的是,当脚本
标记的类型
属性 application / javascript; version = 1.7
。
JavaScript 1.7 adds destructuring assignment, which you're observing there. JavaScript 1.7 is supported by few browsers. Notably, however, it is supported by Mozilla Firefox when the script
tag has a type
attribute of application/javascript;version=1.7
.
这篇关于javascript变量使用数组交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!