https://medium.com/dailyjs/named-and-optional-arguments-in-javascript-using-es6-destructuring-292a683d5b4e


Named and Optional Arguments in JavaScript

Parse your arguments more cleanly using ES6 destructuring

Jim Rottinger
Oct 17, 2016 · 4 min read

Destructuring is perhaps the biggest syntactical change that came to JavaScript in the ES6 specification. While the new syntax may seem weird to many long-time JavaScript programmers, once you are able to wrap your head around it, using it can be very powerful.

If you are not yet familiar with destructuring, it is the ability to map an object literal or array literal to multiple assignment statements at the same time. For example:

Destructuring can be used with array syntax to assign each value in the array to the variable name in the corresponding position of the left-hand side array.

More commonly, however, it can be used with object literals to pull out the properties of the object you want to use as variables.

Note — In the above object literal example, the property names must be the same. Position does not matter here unlike the array example.

If you want to rename something during destructuring, you can use the keyName:newKeyName syntax on the left hand side of your destructuring.

At first, this may just seem like some syntactic sugar on assignment statements, but what makes it much more than that is the ability to assign default values to the variables at the time of destructuring.

This is pretty significant. Say that the right-hand side of the assignment expression is not an object literal but a call to function in the other part of your application. One day, a developer comes along and implements a short-circuiting return statement in that other function and now your call isn’t getting the expected response. Being able to set up defaults at the time of the assignment makes safeguarding your code much easier.

Destructuring Function Parameters

When you pass an argument to a function, before that function begins executing, it assigns the argument you passed in to the corresponding parameter in its function signature. Since it is an assignment statement, that means we can use destructuring to do assign parameters values in a function!

Just as was shown before, we can also rename our keys during destructuring.

Lastly, we can assign default values to both the individual keys in our destructuring statement and the entire block itself.

While this seems a bit tedious to type out, it prevents us from having to check for the existence of every single argument we pass in.

Named and Optional Arguments

If you recall the first example of assigning default values during destructuring and combine that with what we learned in the last section, you might know where I’m going with this. If you can destructure function parameters, and you can assign default values during destructuring, AND the object literal names have to match during the destructuring process, this means that you can have named and optional parameters in your function signature! (so long as you use destructuring). Here is an example:

Conclusion

Hopefully the title of this article was not too misleading. While we do not yet have true named and optional arguments in Javascript in the same way C# does, I have just demonstrated a way to get functionally equivalent behavior using ES6 destructuring. While I do not foresee this pattern replacing positional arguments, it is very nice for situations such as receiving an object in a callback to a promise when making a network call to the server. Instead of hoping the network call returns exactly what you are expecting it to, you can use the pattern described in this post to explicitly define what you are expecting to receive and set up defaults for those values.

Let me know what you think in the comments below!

'C Lang > JS Technic' 카테고리의 다른 글

Airbnb JavaScript 스타일 가이드() {  (0) 2019.10.28
객체와 변경불가성(Immutability)  (0) 2019.10.28
React란 무엇인가  (0) 2019.06.26
삽입정렬, 합병정렬, 선택정렬, 퀵정렬 시간비교  (0) 2019.06.18
MVC패턴  (0) 2019.06.04

+ Recent posts