2017-03-08 6 views
1

私は別の方法で変数の名前を変更していると私はこのリファクタリング変数

を例に

case 'INCREMENT_LIKES' : 
    const index = action.index; 
    return [ 
    ...state.slice(0,index), // before the one we are updating 
    {...state[index], likes: state[index].likes + 1}, 
    ...state.slice(index + 1), // after the one we are updating 
    ] 
case 'INCREMENT_COORDINATES' : 
    const a = action.index; 
    let string = state[a].d || state[a].d2; 
    let array = string.split(" "); 
    let max = array.length; 
    let last = max - 2; 
    let i = (state[a].index || 3) + 1; 
    if (i === last) i = 3; 
    if (array[i] !== 'M' && array[i] !== 'L') array[i] = parseInt(array[i]) + 20; 
    return [ 
    ...state.slice(0,a), // before the one we are updating 
    {...state[a], d: array.join(' '), index: i, d2: array.join(' '), index: i }, // updating 
    ...state.slice(a + 1), // after the one we are updating 
    ] 
case 'INCREMENT_VIEWBOX' : 
    const count = action.index; 
    let string2 = state[count].viewBox; 
    let array2 = string2.split(" "); 
    let max2 = array2.length; 
    let i2 = (state[count].index || 2) + 1; 
    if (i2 === max2) i2 = 2; 
    array2[i2] = parseInt(array2[i2]) - 20; 
    return [ 
    ...state.slice(0,count), // before the one we are updating 
    {...state[count], viewBox: array2.join(' '), index: i2}, 
    ...state.slice(count + 1), // after the one we are updating 
    ] 
default: 
    return state; 
を参照してくださいしたくないこれらの3例では、私のReduxの機能

をリファクタリングする必要があります

これらは3例

const index = action.index; 

const a = action.index; 

const count = action.index; 

同じで別の変数名です

let string = state[a].d || state[a].d2; 

let string2 = state[count].viewBox; 

let array = string.split(" "); 

let array2 = string2.split(" "); 

のためにどのように私はすべての3例に同じ変数名を再利用することができますか?

は、私のような3異なるケースに同じ名前を使用して意味:

const indexからlet string - あなたは中括弧でそれらをラップすることによって、それぞれの場合にスコープを追加することができます

答えて

5

let array

case 'INCREMENT_LIKES' : { 
    const index = action.index; 
    return [ 
    ...state.slice(0,index), // before the one we are updating 
    {...state[index], likes: state[index].likes + 1}, 
    ...state.slice(index + 1), // after the one we are updating 
    ] 
} 

case 'SOME_OTHER_ACTION_TYPE' : { 
    console.log(index) // -> undefined 
} 
関連する問題