You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
92
92
93
93
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.
94
94
95
95
Notice this detail:
96
96
97
-
```js
97
+
```js
98
98
{
99
99
total = 0,
100
100
tax = 0.1,
101
-
tip =0.05} = {}
101
+
tip = 0.05
102
+
} = {}
102
103
```
103
104
104
105
If we don't default our argument Object to an empty Object, and we were to try and run `calculatePrice()` we would get:
105
106
106
-
```js
107
+
``` javascript
107
108
Cannot destructure property `total` of 'undefined' or 'null'.
108
109
```
109
110
110
111
By writing ` = {}` we default our argument to an `Object` and no matter what argument we pass in the function, it will be an `Object`:
111
112
112
-
```js
113
+
```js
113
114
calculatePrice({});
114
115
// 0
115
116
calculatePrice();
@@ -124,17 +125,18 @@ Don't worry about destructuring, we will talk about it in Chapter 10.
124
125
125
126
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
0 commit comments