Javascript switch语句中的const作用域问题
我的减速机中有以下代码。在这两种情况下,findFile()都会返回一个结果,但是,只有在第一次出现时(setImageRotation),才会设置“origFile”。更改第二次出现的“origFile”的名称将允许对其进行设置。我想知道为什么会这样,因为const有一个块级范围Javascript switch语句中的const作用域问题,javascript,reactjs,ecmascript-6,redux,Javascript,Reactjs,Ecmascript 6,Redux,我的减速机中有以下代码。在这两种情况下,findFile()都会返回一个结果,但是,只有在第一次出现时(setImageRotation),才会设置“origFile”。更改第二次出现的“origFile”的名称将允许对其进行设置。我想知道为什么会这样,因为const有一个块级范围 function handler(stateArg, action) { const state = stateArg || {}; let nextState;
function handler(stateArg, action) {
const state = stateArg || {};
let nextState;
switch (action.type) {
case actions.types.setImageRotation: {
const origFile = findFile(action.fileUid, state.files);
const newfile = Object.assign({}, origFile, { rotation: action.rotation });
nextState = updateStateFile(state, newfile);
break;
}
case actions.types.setImageRegionOfInterest: {
const origFile = findFile(action.fileUid, state.files);
const newfile = Object.assign({}, origFile, { roi: action.roi });
nextState = updateStateFile(state, newfile);
break;
}
}
return nextState || state;
}
注:
- origFile未在我的解决方案中的任何其他位置定义李>
- 没有控制台错误
case actions.types.setImageRegionOfInterest:{
var origFile = findFile(action.fileUid, state.files);
var newfile = (0, _assign2.default)({}, origFile, { roi: action.roi });
nextState = updateStateFile(state, newfile);
break;
}
case actions.types.setImageRotation:{
var _origFile = findFile(action.fileUid, state.files);
var _newfile = (0, _assign2.default)({}, _origFile, { rotation: action.rotation });
nextState = updateStateFile(state, _newfile);
break;
}
正如人们在讨论中所说,该代码是有效的。我遇到的是谷歌Chromes调试器和源代码映射的一个bug。当我关闭源映射并调试传输的代码时,我得到了“origfile”的正确值