Skip to content

Commit

Permalink
Merge pull request #19 from samzeng/patch-1
Browse files Browse the repository at this point in the history
Update 03_default_function_arguments.md
  • Loading branch information
AlbertoMontalesi committed Jul 17, 2019
2 parents a8f97f3 + d6845ef commit ebe1f17
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions ebook/03_default_function_arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Prior to ES6, setting default values to function arguments was not so easy. Let's look at an example:

```js
``` js
function getLocation(city,country,continent){
if(typeof country === 'undefined'){
country = 'Italy'
Expand All @@ -26,7 +26,7 @@ When calling `getLocation('Milan')` the second and third parameter (country and

But what if we want our default value to be at the beginning and not at the end of our list of arguments?

```js
``` js
function getLocation(continent,country,city){
if(typeof country === 'undefined'){
country = 'Italy'
Expand All @@ -39,6 +39,7 @@ function getLocation(continent,country,city){

getLocation(undefined, undefined,'Milan')
// Europe Italy Milan

getLocation(undefined,'Paris','France')
// Europe Paris France
```
Expand All @@ -51,7 +52,7 @@ If we want to replace any of the first arguments with our default we need to pas

ES6 makes it very easy to set default function arguments. Let's look at an example:

``` javascript
``` js
function calculatePrice(total, tax = 0.1, tip = 0.05){
// When no value is given for tax or tip, the default 0.1 and 0.05 will be used
return total + (total * tax) + (total * tip);
Expand All @@ -60,14 +61,14 @@ return total + (total * tax) + (total * tip);

What if we don't want to pass the parameter at all, like this:

``` javascript
``` js
// The 0.15 will be bound to the second argument, tax even if in our intention it was to set 0.15 as the tip
calculatePrice(100, 0.15)
```

We can solve by doing this:

``` javascript
``` js
// In this case 0.15 will be bound to the tip
calculatePrice(100, undefined, 0.15)
```
Expand All @@ -76,7 +77,7 @@ It works, but it's not very nice, how to improve it?

With **destructuring** we can write this:

``` javascript
``` js
function calculatePrice({
total = 0,
tax = 0.1,
Expand All @@ -86,30 +87,30 @@ function calculatePrice({

const bill = calculatePrice({ tip: 0.15, total:150 });
// 187.5
```

We made the argument of our function an Object and when calling the function we don't even have to worry about the order of the parameters because they will be matched based on their key.

In the example above the default value for *tip* was 0.05 and we overwrote it with 0.15 but we didn't give a value to tax which remained the default 0.1.

Notice this detail:

```js
``` js
{
total = 0,
tax = 0.1,
tip = 0.05} = {}
tip = 0.05
} = {}
```

If we don't default our argument Object to an empty Object, and we were to try and run `calculatePrice()` we would get:

```js
``` javascript
Cannot destructure property `total` of 'undefined' or 'null'.
```

By writing ` = {}` we default our argument to an `Object` and no matter what argument we pass in the function, it will be an `Object`:
```js
``` js
calculatePrice({});
// 0
calculatePrice();
Expand All @@ -124,17 +125,18 @@ Don't worry about destructuring, we will talk about it in Chapter 10.

Note: We now don't need to construct object and equate to an empty object. Alternative to above we can construct a function as below

``` javascript
``` js
function calculatePrice({
total = 0,
tax = 0.1,
tip = 0.05}){
return total + (total * tax) + (total * tip);
}```
}
```

and the below code would work normal

```js
``` js
calculatePrice({});
// 0
calculatePrice();
Expand Down

0 comments on commit ebe1f17

Please sign in to comment.