added destructuring for rest parameters and formal parameters (#20759)

This commit is contained in:
Neetish Raj
2018-11-02 08:01:24 +05:30
committed by Martin Payne
parent 0ec3c322a4
commit 1023a46311

View File

@ -95,7 +95,29 @@ The arguments object is not available inside the body of an arrow function.
The rest parameter must always come as the last argument in your function definition. The rest parameter must always come as the last argument in your function definition.
```function getIntoAnArgument(arg1, arg2, arg3, ...restOfArgs /*no more arguments allowed here*/) { ```function getIntoAnArgument(arg1, arg2, arg3, ...restOfArgs /*no more arguments allowed here*/) {
//function body //function body
}``` }```
so doing this should throw a SyntaxError: Rest parameter must be last formal parameter
```function invalidRestUse(...args, arg1, arg2){
}```
Rest parameters can be destuctured and unpacked into distinct variables
```
function restDestructuring(...[a, b, c]){
return a + b + c;
}
restDestructuring(1, 2, 3); // 6
```
### ES6 destructuring of arrays in formal parameter
With the new destructuring feature we can even go one step ahead when passing an array to a function and break down its elements as variables.
```
function arrayDestructuring([a, b, c]){
return a + b + c;
}
arrayDestructuring([1,2]) //Nan , as 1 + 2 + undefined = NaN
arrayDestructuring([1,2,3]) // 6
arrayDestructuring([1,2,3,4,5]) // 6 additional elements will be ignored
```