This question already has an answer here:
这个问题已经有了答案:
- Multiple assignment in javascript? What does [a,b,c] = [1, 2, 3]; mean? 4 answers
- 多个任务在javascript中?[a,b,c] = [1,2,3];的意思吗?4答案
var a,b,c;
var arr = [1,2,3];
[a,b,c] = arr;
this code works perfectly in Firefox resulting a=1, b=2 and c=3,
but it doesn't work in Chrome. Is it a Chrome bug or
it is not valid javascript code? (I failed to find it in javascript references)
这段代码在Firefox中工作得很好,结果是a=1, b=2, c=3,但在Chrome中不能用。它是一个Chrome bug还是不是有效的javascript代码?(我在javascript引用中找不到)
How can I modify this code to make it suitable for Chrome, with minimum damage to it?
(I don't really like to write a = arr[0]; b = arr[1]... or the same with arr.shift() all the time)
我如何修改这段代码,使它适合Chrome,并且对它的损害最小?(我不喜欢写a = arr[0];b = arr[1]…或者跟arr。shift()一样)
P.S. this is just an example code, in real code
I get the arr array from somewhere outside my code
这只是一个示例代码,在实际代码中,我从代码之外的地方获得arr数组
2 个解决方案
#1
68
This is a new feature of JavaScript 1.7 called Destructuring assignment:
这是JavaScript 1.7的一个新特性,叫做析构赋值:
Destructuring assignment makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.
析构赋值可以使用映射数组和对象文本结构的语法从数组或对象中提取数据。
The object and array literal expressions provide an easy way to create ad-hoc packages of data. Once you've created these packages of data, you can use them any way you want to. You can even return them from functions.
对象和数组文字表达式提供了一种创建特殊数据包的简单方法。一旦您创建了这些数据包,您就可以任意使用它们。你甚至可以从函数中返回它们。
One particularly useful thing you can do with destructuring assignment is to read an entire structure in a single statement, although there are a number of interesting things you can do with them, as shown in the section full of examples that follows.
对于析构赋值,您可以做的一件特别有用的事情是在一个语句中读取整个结构,尽管您可以使用它们做许多有趣的事情,如下面的示例所示。
You can use destructuring assignment, for example, to swap values:
您可以使用析构赋值,例如,交换值:
var a = 1; var b = 3; [a, b] = [b, a];
This capability is similar to features present in languages such as Perl and Python.
这种功能类似于Perl和Python等语言中的特性。
Unfortunately, according to this table of versions, JavaScript 1.7 has not been implemented in Chrome. But it should be there in:
不幸的是,根据这个版本表,JavaScript 1.7还没有在Chrome中实现。但它应该存在:
- FireFox 2.0+
- FireFox 2.0 +
- IE 9
- IE 9
- Opera 11.50.
- Opera 11.50。
Try it for yourself in this jsfiddle: http://jsfiddle.net/uBReg/
在这个jsfiddle中可以自己尝试:http://jsfiddle.net/uBReg/
I tested this on Chrome (failed), IE 8 (failed), and FireFox 5 (which worked, per the wiki table).
我在Chrome(失败)、IE 8(失败)和FireFox 5(每个wiki表都能运行)上进行了测试。
#2
7
It is possible only for Javascript 1.7 as already answered by @Justin. Here is a trial to simulate it in the widespread browsers:
@Justin已经回答了Javascript 1.7的问题。这里有一个在广泛的浏览器中模拟它的试验:
function assign(arr, vars) {
var x = {};
var num = Math.min(arr.length, vars.length);
for (var i = 0; i < num; ++i) {
x[vars[i]] = arr[i];
}
return x;
}
var arr = [1, 2, 3];
var x = assign(arr, ['a', 'b', 'c']);
var z = x.a + x.b + x.c; // z == 6
I don't know how useful it is.
我不知道它有多有用。
#1
68
This is a new feature of JavaScript 1.7 called Destructuring assignment:
这是JavaScript 1.7的一个新特性,叫做析构赋值:
Destructuring assignment makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals.
析构赋值可以使用映射数组和对象文本结构的语法从数组或对象中提取数据。
The object and array literal expressions provide an easy way to create ad-hoc packages of data. Once you've created these packages of data, you can use them any way you want to. You can even return them from functions.
对象和数组文字表达式提供了一种创建特殊数据包的简单方法。一旦您创建了这些数据包,您就可以任意使用它们。你甚至可以从函数中返回它们。
One particularly useful thing you can do with destructuring assignment is to read an entire structure in a single statement, although there are a number of interesting things you can do with them, as shown in the section full of examples that follows.
对于析构赋值,您可以做的一件特别有用的事情是在一个语句中读取整个结构,尽管您可以使用它们做许多有趣的事情,如下面的示例所示。
You can use destructuring assignment, for example, to swap values:
您可以使用析构赋值,例如,交换值:
var a = 1; var b = 3; [a, b] = [b, a];
This capability is similar to features present in languages such as Perl and Python.
这种功能类似于Perl和Python等语言中的特性。
Unfortunately, according to this table of versions, JavaScript 1.7 has not been implemented in Chrome. But it should be there in:
不幸的是,根据这个版本表,JavaScript 1.7还没有在Chrome中实现。但它应该存在:
- FireFox 2.0+
- FireFox 2.0 +
- IE 9
- IE 9
- Opera 11.50.
- Opera 11.50。
Try it for yourself in this jsfiddle: http://jsfiddle.net/uBReg/
在这个jsfiddle中可以自己尝试:http://jsfiddle.net/uBReg/
I tested this on Chrome (failed), IE 8 (failed), and FireFox 5 (which worked, per the wiki table).
我在Chrome(失败)、IE 8(失败)和FireFox 5(每个wiki表都能运行)上进行了测试。
#2
7
It is possible only for Javascript 1.7 as already answered by @Justin. Here is a trial to simulate it in the widespread browsers:
@Justin已经回答了Javascript 1.7的问题。这里有一个在广泛的浏览器中模拟它的试验:
function assign(arr, vars) {
var x = {};
var num = Math.min(arr.length, vars.length);
for (var i = 0; i < num; ++i) {
x[vars[i]] = arr[i];
}
return x;
}
var arr = [1, 2, 3];
var x = assign(arr, ['a', 'b', 'c']);
var z = x.a + x.b + x.c; // z == 6
I don't know how useful it is.
我不知道它有多有用。