JavaScript 中的多重赋值? `[ a, b, c ] = [ 1, 2, 3 ]` 是什么意思?

发布于 2024-09-28 14:19:02 字数 280 浏览 1 评论 0原文

在一个项目中,开发人员向我们发送了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃素的狼 2024-10-05 14:19:02

这是一项名为解构赋值的功能,已添加到 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

凉城已无爱 2024-10-05 14:19:02

以下是有关该主题的更新:自 JavaScript 版本 1.7,所有主流浏览器都支持解构赋值:请参阅 浏览器兼容性

解构赋值语法是一种 JavaScript 表达式,可以将数组中的值或对象中的属性解压到不同的变量中。

MDN 文档

所以你可以这样做:

let a, b;
[a, b] = ["Hello", "World"];

console.log(a); // "Hello"
console.log(b); // "World"

或者简单地在如果要定义变量,则一行:

let [a, b] = ["Hello", "World"];

console.log(a); // "Hello"
console.log(b); // "World"

Here’s an update on the subject: as of JavaScript version 1.7, destructuring assignments are supported by all major browsers: see browser compatibility.

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

MDN’s documentation

So you can do:

let a, b;
[a, b] = ["Hello", "World"];

console.log(a); // "Hello"
console.log(b); // "World"

Or simply in one line if you're defining the variables:

let [a, b] = ["Hello", "World"];

console.log(a); // "Hello"
console.log(b); // "World"
凡间太子 2024-10-05 14:19:02

这是解构赋值,在 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

开始看清了 2024-10-05 14:19:02

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文