function mainFunc(mainParameter) {
return {
subFunc1 : function (sub1Parameter) {
return sub1Parameter;
},
subFunc2 : function (sub2Parameter) {
return sub2Parameter
}
}
}
// This works
console.log(mainFunc().subFunc1('example text'));
// I need this
console.log(mainFunc('example text'));
How can i use ‘return’ with mainFunc() like last line in the code block.
2
Answers
You can add an
if
to see ifmainFunc
was called with any arguments, then ifmainFunc
was called with an argument, have it do whatever it is it’s supposed to do with that parameter instead of returning the object, like this:if you need only the second type of output
console.log(mainFunc('example text'));
you can do this way:Otherwise this one works in both cases: