Skip to content Skip to sidebar Skip to footer

What Does Passing Arguments In This Way Mean?

I'm learning about javascript and I see this block of code that I don't understand: exports.configure = ({ expressapp = null, userdb = null, path = '/myroute' } = {}) =>

Solution 1:

The pattern is a destructuring assignment which assigns a plain object as the default parameter, to avoid TypeError if no value is passed to the function.

constexports = {};

exports.configure = ({
   expressapp = null,
   userdb = null,
   path = '/myroute'
}) => { 
   // handle routesconsole.log(expressapp)
};

try {
  exports.configure();
} catch(err) {
  console.error(err)
}

constexports = {};

exports.configure = ({
   expressapp = null,
   userdb = null,
   path = '/myroute'
} = {}) => { 
   // handle routesconsole.log(expressapp)
};

try {
  exports.configure();
} catch(err) {
  console.error(err)
}

Solution 2:

You should read this page for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Basically if we assign

var [a,b] = [2,3]; 

it makes sense that we get a=2 and b=3. Think of it as a mirror.

And if we assign

var {a:x, b:y} = {a:3, b:4} 

it makes sense that x=3 and y=4 (because the 'a' position on {a: x, b: y} is occupied by x and the 'a' position on {a: 3, b: 4} is occupied by 3, so it would make logical sense to assign 3 to x.)

A) This can be extended to function parameters, where we can have

functionmyFunc({a:x, b:y}){//do stuff}

and calling myFunc({a: 3, b: 4}) would imply x = 3, y = 4


B) We now ask ourselves why we need to introduce an x and y. Instead we could just have

functionmyFunc({a:a, b:b}){//do stuff}

and calling myFunc({a: 3, b: 4}) would imply a = 3, b = 4


C) What if we have missing information? Then we could do

functionmyFunc({a:a = 1, b:b = 2}){// do stuff}

Now calling myFunc({b:4}) would imply a = 1, b = 4. And calling myFunc({}) would imply a = 1, b = 2. Because you can think of {} as {a: undefined, b: undefined}


D) What if the entire argument (the entire object) was missing? That's an entirely different thing altogether, and would cause an error. To answer that lets do a simpler example.

functionsimple(a=3){//do stuff};

Calling simple() would trigger the default argument, implying a=3

Going back to our more complex function, we can write

functionmyFunc({a:a = 1, b:b = 2} = {}){// do stuff}

Similarly, calling myFunc() would trigger the default argument, implying

{a:a = 1, b:b = 2} = {}

and you can think of that as

{a:a = 1, b:b = 2} = {a: undefined, b: undefined}

which makes 'a' undefined, and 'b' undefined, triggering the default argument, which implies a = 1, b = 2


E) Now we introduce a shorthand where

var {a:a} = {a:3}

is the same as

var {a} = {a: 3}

Going back to our function, we can see that

functionmyFunc({a:a = 1, b:b = 2} = {}){// do stuff}

is equivalent to

functionmyFunc({a = 1, b = 2} = {}){// do stuff}

This doesn't add any functionality, just cleans things up

Solution 3:

This is the same as writing

exports.configure = (argument = {}) => { 
   // handle routes
};

where argument is

{
   expressapp = null,
   userdb = null,
   path = '/myroute'
}

Post a Comment for "What Does Passing Arguments In This Way Mean?"