Reputation: 194
How I can set an object as function param with default values
Example:
function objasparam({obj1: {var1: 0, var2: "something"}, obj2: {...}}) {
console.log(obj1.var1); //the expacted log is 0 but it is undefined
}
objasparam({
obj1:{
var2: "other"
}
});
even if I do it this way there is an error
function objasparam(obj = {obj1: {var1: 0, var2: "something"}, obj2: {...}}) {
console.log(obj.obj1.var1); //the expacted log is 0 but it is undefined
}
objasparam({
obj1:{
var2: "other"
}
});
Upvotes: 1
Views: 321
Reputation: 2548
You should add an default object. So you are not forced to have all parameters. If you have no arguments, the defaults exists.
const objasparam = (config) => {
let defaults = {
obj1: {
var1: 0,
var2: "something"
},
obj2: {
var1: 1
}
}
config = {
...defaults,
...config
}
// to get your obj1.var1
console.log(config.obj1.var1);
// complete config
console.log(config);
}
objasparam();
objasparam({
obj1: { var1: 22, var2: "another string"}
});
.as-console-wrapper { max-height: 100% !important }
Upvotes: 1
Reputation: 1073968
There are two levels for defaults there:
Defaulting the properties of the object when destructuring it,
Providing a default for the object as a whole.
The first is via destructuring defaults, the second is via parameter defaults. An example is probably the best way to show this. I'll do one with just one parameter to keep it simple, but you can do it with multiple parameters as well:
function objasparam(
// Parameter default ------------vvvv
{prop1 = 0, prop2 = "something"} = {}
// ----^^^--------^^^^^^^^^^^^^---- destructuring defaults
) {
console.log(`prop1 = ${prop1}, prop2 = ${prop2}`);
}
console.log("Calling the function with no parameter at all:");
objasparam();
console.log("Calling it with {prop1: 42}:");
objasparam({prop1: 42});
console.log("Calling it with {prop2: 'answer'}:");
objasparam({prop2: 'answer'});
console.log("Calling it with {prop1: 42, prop2: 'answer'}:");
objasparam({prop1: 42, prop2: 'answer'});
/* Make the console take up the whole result pane */
.as-console-wrapper {
max-height: 100% !important;
}
Of course, if you want to require an object gets passed in, leave of the parameter default.
function objasparam(
{prop1 = 0, prop2 = "something"}
// ----^^^--------^^^^^^^^^^^^^---- destructuring defaults
) {
console.log(`prop1 = ${prop1}, prop2 = ${prop2}`);
}
try {
console.log("Calling the function with no parameter at all:");
objasparam(); // Fails because an object is expected
} catch (error) {
console.error(error);
}
console.log("Calling it with {prop1: 42}:");
objasparam({prop1: 42});
console.log("Calling it with {prop2: 'answer'}:");
objasparam({prop2: 'answer'});
console.log("Calling it with {prop1: 42, prop2: 'answer'}:");
objasparam({prop1: 42, prop2: 'answer'});
/* Make the console take up the whole result pane */
.as-console-wrapper {
max-height: 100% !important;
}
Or if you only want to provide defaults for the case where the object isn't given at all, and not if it is, leave off the destructuring defaults:
function objasparam(
// Parameter default ------------vvvv
{prop1, prop2} = {prop1: 0, prop2: "something"}
) {
console.log(`prop1 = ${prop1}, prop2 = ${prop2}`);
}
console.log("Calling the function with no parameter at all:");
objasparam();
console.log("Calling it with {prop1: 42}:");
objasparam({prop1: 42});
console.log("Calling it with {prop2: 'answer'}:");
objasparam({prop2: 'answer'});
console.log("Calling it with {prop1: 42, prop2: 'answer'}:");
objasparam({prop1: 42, prop2: 'answer'});
/* Make the console take up the whole result pane */
.as-console-wrapper {
max-height: 100% !important;
}
Finally: This can be nested, since destructuring allows nesting. Re-reading your question, I'm wondering if that's what you were trying to do:
function objasparam(
{ // vvvv--- default for if the object has no `obj1`
obj1: {prop1 = 0, prop2 = "something"} = {},
obj2: {prop3 = "three", prop4 = "four"} = {}
{ // ^^^^--- default for if the object has no `obj2`
} = {} // <=== parameter default if nothing is passed
) {
console.log(`prop1 = ${prop1}`);
console.log(`prop2 = ${prop2}`);
console.log(`prop3 = ${prop3}`);
console.log(`prop4 = ${prop4}`);
}
console.log("Calling the function with no parameter at all:");
objasparam();
console.log("Calling it with {obj1: {prop1: 42}}:");
objasparam({obj1: {prop1: 42}});
console.log("Calling it with {obj2: {prop4: 'quattro'}}:");
objasparam({obj2: {prop4: 'quattro'}});
console.log("Calling it with {obj1: {prop1: 42}, obj2: {prop4: 'quattro'}}:");
objasparam({obj1: {prop1: 42}, obj2: {prop4: 'quattro'}});
/* Make the console take up the whole result pane */
.as-console-wrapper {
max-height: 100% !important;
}
Upvotes: 2