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

时间:2016-11-11 05:13:01

标签: javascript redux

我刚学完javascript和打字稿(我知道redux不是打字稿),但是对于redux滤镜的语法一直很困惑:

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

我似乎是javascript中的默认变量,但只有当默认变量是位置变量之后。这里发生了什么?有人能指出我解释这种语法的文档吗?

1 个答案:

答案 0 :(得分:2)

基本上它意味着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' }));