gpt4 book ai didi

javascript - redux 中的命名变量过滤器语法

转载 作者:行者123 更新时间:2023-12-02 13:58:35 24 4
gpt4 key购买 nike

我刚刚学习完 javascript 和 typescript(我知道 redux 不是 typescript),但对 redux 过滤器的语法感到非常困惑:

function visibilityFilter(state = 'SHOW_ALL', action) {
...
}

我在javascript中似乎有默认变量,但只有当默认变量位于位置变量之后时。这里发生了什么?有人可以向我指出解释此语法的文档吗?

最佳答案

基本上,这意味着 reducer 期望接收一个操作,但如果第一个参数是未定义,则使用默认值。

function reducer(state = 'initial', action) {
if (action.type === 'change') {
return 'new';
}

return state;
}

console.log(
'uses given initial state: ',
reducer('what', {})
);

console.log(
'null is also considered a given state: ',
reducer(null, {})
);

console.log(
'uses default state if first param is undefined: ',
reducer(undefined, {})
);

const myServerState = undefined;
console.log(
'passing undefined might not be so obvious: ',
reducer(myServerState, {})
);

// throws error because action is undefined
console.log(reducer({ type: 'change' }));

关于javascript - redux 中的命名变量过滤器语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com