简体   繁体   English

Redux中的命名变量过滤器语法

[英]named variable filter syntax in redux

I have just finished learning javascript and typescript (I know redux is not typescript), but have been thouroughly confused by the syntax for redux filters: 我刚刚学习完javascript和打字稿(我知道redux不是打字稿),但是一直被redux过滤器的语法迷惑不解:

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

I've seem default variables in javascript, but only when the default variable is AFTER the positional variable. 我似乎在javascript中使用了默认变量,但仅当默认变量在位置变量之后才使用。 What is going on here? 这里发生了什么? Can someone point me to documentation that explains this syntax? 有人可以指向我说明该语法的文档吗?

Basically it means that the reducer expects to receive an action, but uses the default value if the first parameter is undefined . 基本上,这意味着reducer希望收到一个动作,但是如果第一个参数undefined ,则使用默认值。

 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' })); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM