0

Okay so there's a lot of similar questions here but none that actually address this. What if I want to save the default parameters of a function on a separated object, in a separated file and then I want the function to use this default parameters only if not specified when calling it?

Example:

export const defaultParameters = { a: "foo", b: "bar"}

And then in another file:

import {defaultParameters} from ./utils.js

myFunction = function(defaultParameters = {}){
   const {a, b} = defaultParameters;
   console.log(a,b);
}

What I want for it to do is that calling

myFunction({a: "hello"})

should print

"hello", "bar"

But that's not how that works and I'm trying to figure this out. Anyone knows how to achieve this?

3
  • 2
    Can you use const { a,b } = { ...defaultParameters, ...options }. Since options comes last in that it will override any other parameters. You just made a merged object that you can query. Commented Jun 1, 2022 at 14:18
  • By naming your function's parameter defaultParameters, you're overwriting the imported one. Commented Jun 1, 2022 at 14:19
  • 1
    You probably can't find anything because what you ask for is impossible. Moreover, an XY problem. Seems like the real thing you're after is have "default fallback values for a parameter object" which is both possible and would give you relevant results. Commented Jun 1, 2022 at 14:20

1 Answer 1

4

You won't be able to do it directly in the parameter, but you can inside the function..

eg..

import {defaultParameters} from ./utils.js

myFunction = function(params = {}){
   const {a, b} = {...defaultParameters, ...params};
   console.log(a,b);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Well, its actually ugly. Some value depends on some outer value. Usually you try to make a function as pure as possible
Thanks @Keith! I got it to work. @bill.gates why do you think its ugly? What do you mean by a "pure" function? Do you have any alternative way you would approach this problem?
@DanielGuedes pure means the function return values are identical for identical arguments. This function has side-effects. If defaultParameters are changed or missing you receive different results by the same input. Sometimes you cannot achieve to make a function pure, but you should try getting towards purity. alternatives would be to pass defaultParameters as an second argument like (params = {}, defaultParameters = {}) and then you pass it when you call it.
@bill.gates defaultParameters, is obviously a global constant, as such this function is indeed pure, there would be no side effects.
Oh I see, but in my application that's actually my goal. If something is not specified I want the function to grab it from defautParemeters, and as @Keith mentioned it is going to be a global constant and I'm not going to change it dynamically, but I still want the option to manually change it from time to time as my code unravel. Thank you! Never got as quick and efficient reponse as for this question!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.