Just JavaScript - Parameters, and Destructuring |
Written by Ian Elliot | |||
Monday, 05 March 2018 | |||
Page 2 of 2
Rest (ES2015)You can find out how many parameters are specified in a functions declaration using the function’s length property. For example:
is 3. This is the number of parameters specified in the declaration not the number of arguments in the use of the function. Notice that using the function length and arguments length properties we can easily work out how many more or fewer arguments have been supplied. For example:
This will add the first three arguments using the parameters a,b and c and if there are any more it adds them from the arguments object. To make this sort of thing even easier ES2015 added the rest parameter. If the last parameter of a function is prefixed with … then any additional arguments provided are stored in the final parameter as an Array. So the previous function could be written:
Notice that you don’t have to call the final parameter rest only the three dots matter. You don’t need the rest feature but it is slightly easier. Default Parameters (ES2015)JavaScript is so flexible that often we allow optional parameters that are set to a default if the user doesn’t make use of them. Obviously as JavaScript’s parameters are positional any optional parameters have to be last in the parameters list. The traditional way of dealing with optional parameters is to test for undefined values. For example:
If you don’t supply a value for b then it is set to 1. ES2015 introduced default parameter values which work in much the same way. For example:
sets b to 1 if it isn’t provided. Notice that ES2015 default parameters are compiled to exactly the code given earlier. That is if you pass in undefined then the default value will still be used but any other value including null etc, will not be replaced by the default. Also notice that the default value can be any valid expression including function calls and this is evaluated each time the function is called. These default expression are evaluated in order of the parameters and earlier defaults can be used in later parameter defaults. For example:
b is set to a+1 if it isn’t supplied. Destructuring (ES2015)Although destructuring is a general facility it has some particularly important used in connection with functions and it can be seen as a generalization of the way the arguments object is unpacked or destructured into the parameter variables. The basic idea of destructuring is that it will unpack the values in an array or the properties in an object into individual variables. The syntax is simply to assign to a structure of the same type but with variables rather than values. For example:
or
assigns 1 to a and 2 to b. Notice that [1,2] is an array literal but the destructuring works with a general Array object. Destructuring an object is very similar:
This assigns 1 to a and 2 to b. Notice that you have to use the property names to determine which property maps to which variable. For example:
assigns 2 to a and 1 to b. As a shorthand you can destructure to variables with the same name as the properties. For example
which is a short form of
You should be able to see that Array destructuring is a lot like the way arguments is unpacked into the parameter variables and the similarity is more than skin deep. For example, you don’t have to unpack all of the elements of an Array, you can use the rest notation to set an Array to take any part of the source Array that isn’t used and finally you can set default values. For example:
assigns 1 to a, 2 to b, 3 to c and d is an Array of length 0 and
assigns 0 to a, 1 to b, 2 to c and d is [3,4,5].. Notice that the default value is only used if the source element is undefined. Also keep in mind that default values can be arbitrary expressions that evaluate to objects. So to summarize:
This is an extract from the book Just JavaScript by Ian Elliot. Buy Now: from your local AmazonJust JavaScript
|
|||
Last Updated ( Monday, 05 March 2018 ) |