javascript变量使用数组交换 [英] javascript variable swapping using arrays

查看:56
本文介绍了javascript变量使用数组交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 [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屋!

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