why let an object equals to an empty object like this ?
export const addChat = ({
// cuid is safer than random uuids/v4 GUIDs
// see usecuid.org
id = cuid(),
msg = '',
user = 'Anonymous',
timeStamp = Date.now()
} = {}) => ({
type: ADD_CHAT,
payload: { id, msg, user, timeStamp }
});
jenovs
2
It’s a default value in case addChat
is called without arguments.
Thanks
Why the order like that? Put the empty object on the right
It should be:
function (a=5)
function (object={…} )
Or
function ({}={…})
That is how default function parameters look, not to mention any normal assignment variable = value
function logValue(obj = {}) {
console.log(obj);
}
logValue()
// {}
logValue({
name: 'Jonh Doe'
})
// {name: "Jonh Doe"}
Thanks, I’m understand it for right now.
Default values for parameters and default values for variables
// default values for variables
function move({x = 0, y = 0} = {}) {
return [x, y];
}
move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, 0]
move({}); // [0, 0]
move(); // [0, 0]
// Default values for parameters
function move({x, y} = { x: 0, y: 0 }) {
return [x, y];
}
move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, undefined]
move({}); // [undefined, undefined]
move(); // [0, 0]