Can we set persistent default parameters which remain set until explicitly changed?

The below is a function fn where expected result is for a, b, c to defined at every call of fn, whether an object parameter is passed or not. If object is passed which sets property, property should be set only for that object.

const fn = (opts = {a:1, b:2, c:3}) => console.log(opts);

when called without parameters the result is

fn() // {a: 1, b: 2, c: 3}

when called with parameter, for example {b:7}, the expected result is

fn({b:7}) // {a: 1, b: 7, c: 3}

however, the actual result is

fn({b:7}) // {b: 7}

Was able to get expected result by defining an object outside of function and using Object.assign() within function body

const settings = {a: 1, b: 2, c: 3};
const fn = opts => {opts = Object.assign({}, settings, opts); console.log(opts)}
fn({b: 7}) // {a: 1, b: 7, c: 3}
fn(); // {a: 1, b: 2, c: 3}
/*
  // does not log error; does not return expected result
  const fn = (opts = Object.assign({}, settings, opts)) => console.log(opts)
 
*/

Can the above result be achieved solely utilizing default parameters, without defining an object to reference outside of function parameters or within function body?


Solution 1:

Maybe I misunderstood the question, but you seem to be looking for default initialisers for each separate property. For that, you have to use destructuring:

const fn = ({a = 1, b = 2, c = 3} = {}) => console.log({a, b, c});

If you want to keep arbitrary properties, not just those that you know of up front, you might be interested in the object rest/spread properties proposal that allows you to write

const fn = ({a = 1, b = 2, c = 3, ...opts} = {}) => console.log({a, b, c, ...opts});

Can an opts variable as the single object reference be achieved solely utilizing default parameters, without defining an object to reference outside of function parameters or within function body?

No. Parameter declarations are only able to initialise variables with (parts of) the arguments, and possibly (as syntactic sugar) with default values when no or undefined argument (parts) are passed. They are not able to carry out unconditional computations and create variables inialised from the results - which is what you attempt to achieve here.

You are supposed to use the function body for that.

Solution 2:

No

The best that can be done is either your own answer or this:

const fn = (default_parameters) => { 
  default_parameters = Object.assign({}, {a: 1, b: 2, c: 3},default_parameters);
  console.log('These are the parameters:');
  console.log(default_parameters);
}

    fn();

    fn({b: 7});

    fn({g: 9, x: 10});

The default parameter block is only executed if the value is not set, so your own answer is the best that is on offer ie use two parameters

You can convince yourself of this by creating a code block that will fail if executed and testing that passing a parameter works (to show that the code block is not executed) and testing that not passing a parameter fails (showing that the code block is only executed when no parameter is passed).

This should demonstrate clearly that any paramter passed will prevent the default parameter from being evaluated at all.

    const fn = (default_parameters = (default_parameters = Object.assign({}, {a: 1, b: 2, c: 3},default_parameters))) => { 
      
      console.log('These are the parameters:');
      console.log(default_parameters);
    }

        fn({b: 7});
        
        fn();

        fn({g: 9, x: 10});