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, {
pleted: !statepleted
})
default:
return state
}
}
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, {
pleted: !state.pleted
})
default:
return state
}
}
Share
Improve this question
edited Jun 12, 2017 at 8:58
thor
asked Jun 12, 2017 at 4:42
thorthor
22.5k31 gold badges102 silver badges191 bronze badges
5
- 6 It makes no sense. The default parameters must e last. For that reason in some piled languages it's even denied. – zerkms Commented Jun 12, 2017 at 4:47
- Why can't you "default the next parameter"? – RobG Commented Jun 12, 2017 at 4:57
- @RobG I didn't e up with the example. I am just trying to understand a use case in Redux tutorial. – thor Commented Jun 12, 2017 at 9:00
- @zerkms see my answer below to understand the redux use case. – cquezel Commented Mar 8, 2018 at 17:27
- @cquezel my ment was not about whether it technically is possible or not, but about the design perspective of doing so. – zerkms Commented Mar 8, 2018 at 19:02
4 Answers
Reset to default 8The 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
}
The defaults are called when the parameter is undefined
:
todo(undefined, { type: 'WHATEVER' });
To prevent the need for setting undefined
s 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 } = {}) => {};
Default parameter has to e last i dont think there is a direct way to make them e 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);
}
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.