下面是es5语法
function customMsg(state, action) {
state = state || {};
return $.extend({}, state, {
isFetching: false,
didInvalidate: false,
checkStatus: checkStatus(state.checkStatus, action)
});
}
function checkStatus(state, action) {
state = state || {
isFetching: false,
didInvalidate: false,
type: "room"
};
return state;
}
}
下面是es6语法
const initialStae = {
isFetching: false,
didInvalidate: false,
checkStatus: checkStatus(state.checkStatus, action)
}
function customMsg(state = initialStae, action) {
return state;
}
function checkStatus(state, action) {
state = state || {
isFetching: false,
didInvalidate: false,
type: "room"
};
return state;
}
为什么我的第7行会从es6中显示“状态未定义”?
customMsgReducer.js:36Uncaught ReferenceError: state is not defined(…)
答案 0 :(得分:1)
在ES5中,您定义state = state || {};
,然后访问state.checkStatus
。因此它的工作。
function customMsg(state, action) {
state = state || {};
return $.extend({}, state, {
isFetching: false,
didInvalidate: false,
checkStatus: checkStatus(state.checkStatus, action)
});
}
但是在ES6中,您访问state.checkStatus
而未在此处定义state
undefined
。因此,访问checkStatus
的{{1}}会导致错误。