简体   繁体   English

这个JavaScript构造叫什么

[英]What is this JavaScript construct called

I've found a JavaScript arrow function that looks something like this: 我发现一个JavaScript箭头函数看起来像这样:

([a,b,c]) => {
  a = 1;
  b = 2;
  c = 'x';
}

How is this function invoked? 如何调用此函数? Also, what is this construct called? 另外,这个结构叫做什么?

This is an arrow function, which gets an array as a parameter and destruct the first 3 values into the corresponding parameters - a,b,c . 这是一个箭头函数,它获得一个数组作为参数并将前三个值破坏为相应的参数-a a,b,c But it must be assigned to a variable or be self invoked. 但是必须将其分配给变量或自行调用。

() => {} - Arrow function () => {} - 箭头功能

[a,b,c] - Array destructuring [a,b,c] - 数组解构

Example

 const func = ([a,b,c]) => { console.log(a); console.log(b); console.log(c); }; func([1,2,3,4]); 

([a, b, c]) => {}

The first part is a destructuring assignment in the parameters, which takes an array as parameter and returns the variables with the values of the position. 第一部分是参数中的解构分配 ,它以数组为参数,并返回带有位置值的变量。

The later assignment makes no sense with the given code. 对于给定的代码,后面的分配是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM