thor
thor

Reputation: 22570

es6 how to use default parameters that go before non-default parameters?

I am a bit rusty on default parameters, and I am wondering how can I use a default value for a parameter if it goes before parameters without defaults?

In the example from Redux.js below, when will the default value {} for the state parameter be useful? (since you can't default the next parameter)?

const todo = (state = {}, action) => {
  switch (action.type) {
    //...

    case 'TOGGLE_TODO':
      if (state.id !== action.id) {
        return state
      }

      return Object.assign({}, state, {
        completed: !state.completed
      })

    default:
      return state
  }
}

Upvotes: 8

Views: 5676

Answers (4)

cquezel
cquezel

Reputation: 4497

I'm a newbe to javascript but if I am not mistaken, default parameters simply replace 'undefined' parameters.

if I have a function defined as:

function example(var1 = false, var2, var3 = false) ...

This means that the following calls are all legal :

example(true, someVar, true);
example(true, someVar); // calls  example(true, someVar, false)
example(undefined, someVar, true); // calls example(false, someVar, true)
example(undefined, someVar); // calls example(false, someVar, true)

The redux framework simply passed undefined explicitly.

Upvotes: 0

laser
laser

Reputation: 1376

The usage in question is specific to redux.js. The default value for the first parameter is generally useless in function calls because of the second parameter without default.

However, as said earlier in the same tutorial about Reducers:

Redux will call our reducer with an undefined state for the first time. This is our chance to return the initial state of our app:

function todoApp(state, action) {
  if (typeof state === 'undefined') {
    return initialState
  }
  //...
  return state
}

So the 1st parameter isn't really omitted here. Redux is supplying undefined as its value on initialization. It is only in this case, the tutorial used default arguments syntax as a shortcut:

function todoApp(state = initialState, action) {
  //...
  return state
}

Upvotes: 8

Ori Drori
Ori Drori

Reputation: 193348

The defaults are called when the parameter is undefined:

todo(undefined, { type: 'WHATEVER' });

To prevent the need for setting undefineds when calling the function, I prefer to destructure an object with defaults. Using an object make the order of the params irrelevant.

todo({ state = {}, action } = {}) => {};

Upvotes: 4

Shivam Tiwari
Shivam Tiwari

Reputation: 355

Default parameter has to come last i dont think there is a direct way to make them come before other parameters however you can use Arguments Object to achieve something like this.

e.g

function myFunction(){
  var firstParam , secondParam;
  if(arguments.length === 0){
     console.log("no input");
     return;
  }
  if(arguments.length === 1){
    secondParam =  arguments[0];
  }
  else{
    firstParam  =  arguments[0];
    secondParam =  arguments[1];
  }
   // you can write any logic above
   // also you can give params in function definition as well myFunction(firstParam , secondParam)
   // use params as you wish
  console.log(firstParam);
  console.log(secondParam);
}

Upvotes: 0

Related Questions