简体   繁体   中英

What is this JavaScript construct called

I've found a JavaScript arrow function that looks something like this:

([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 . But it must be assigned to a variable or be self invoked.

() => {} - Arrow function

[a,b,c] - Array destructuring

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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