JavaScript 中的多重赋值? `[ a, b, c ] = [ 1, 2, 3 ]` 是什么意思?
在一个项目中,开发人员向我们发送了一个 JS 文件,其代码类似于:
var myList = [ 1, 2, 3 ];
var a, b, c;
[ a, b, c ] = myList;
它适用于 Opera 10.30 和 Firefox 3.6.x,但不适用于 Opera 10.60 和 Chrome。
只是好奇:您是否有任何参考或链接表明此代码是否符合 ECMAScript 标准?
For a project, a developer sent us a JS file with code similar to this:
var myList = [ 1, 2, 3 ];
var a, b, c;
[ a, b, c ] = myList;
It works in Opera 10.30, and Firefox 3.6.x, but it’s not okay for Opera 10.60, and Chrome.
It’s just curiosity: do you have any reference or link that says this code is compliant to the ECMAScript standard or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一项名为解构赋值的功能,已添加到 JavaScript 1.7 和 ECMAScript 6 中。它不是 ECMAScript 5 的一部分:JavaScript 1.7 新功能的跨浏览器支持是什么?特别是数组推导式和“let”语句
This is a feature called destructuring assignment, which was added in JavaScript 1.7 and ECMAScript 6. It is not a part of ECMAScript 5: What is cross browser support for JavaScript 1.7's new features? Specifically array comprehensions and the "let" statement
以下是有关该主题的更新:自 JavaScript 版本 1.7,所有主流浏览器都支持解构赋值:请参阅 浏览器兼容性。
所以你可以这样做:
或者简单地在如果要定义变量,则一行:
Here’s an update on the subject: as of JavaScript version 1.7, destructuring assignments are supported by all major browsers: see browser compatibility.
So you can do:
Or simply in one line if you're defining the variables:
这是解构赋值,在 Javascript 1.7 (mozilla) 和一些较新的浏览器中可用: http://www .robertnyman.com/javascript/javascript-1.7.html#destructuring-赋值
This is destructuring assignment, available in Javascript 1.7 (mozilla) and some newer browsers: http://www.robertnyman.com/javascript/javascript-1.7.html#destructuring-assignment
Opera 较旧的“futhark”JavaScript 引擎对此有支持,但它在新引擎“carakan”中被删除,因为它是非标准的,在网络上不需要,并且会使新的非常快速的实现变得复杂。
Opera's older "futhark" JavaScript engine had support for this, but it was dropped in the new engine "carakan", because it was non-standard, not required on the web, and would complicate the new and very fast implementation.