在ES6中将数组解构为函数参数的语法 [英] Syntax for destructuring arrays into function parameters in ES6
本文介绍了在ES6中将数组解构为函数参数的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于如何使用Javascript 2015/ES6/ECMAScript 2015中的函数参数传递结构的对象,有很多文档,
There is plenty of documentation on how to destructure objects passed as function parameters in Javascript 2015 / ES6 / ECMAScript 2015, with a function like this:
function foo({a, b}) {
console.log(`a: ${a}, b: ${b}`);
}
但是如何解构 array 参数?
推荐答案
解构数组参数的正确语法是:
The correct syntax to destructure an array parameter is:
function foo([a, b]) {
console.log(`param1: ${a}, param2: ${b}`);
}
可以这样称呼:
foo(['first', 'second']);
// Will output:
// param1: first, param2: second
根据 探索ES6 ,第11.6节,您也可以使用它来破坏箭头函数中的参数:
According to Exploring ES6, section 11.6, you can use this to destructure parameters within arrow functions as well:
const items = [ ['foo', 3], ['bar', 9] ];
items.forEach(([word, count]) => {
console.log(word+' '+count);
});
这篇关于在ES6中将数组解构为函数参数的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文