搜尋

首頁  >  問答  >  主體

javascript - js中括號問題

import {INCREMENT} from "./types"

const mutations = {
    [INCREMENT] (state) {
      state.count++;
    }
}

[INCREMENT] INCREMENT是變數直接使用不就行了嗎,為什麼還要加一個中括號呢?

高洛峰高洛峰2833 天前675

全部回覆(2)我來回復

  • 黄舟

    黄舟2017-05-16 13:44:12

    [INCREMENT]是计算INCREMENT这个变量的值作为函数名,不使用中括号是把INCREMENT這個字串作為函數名。

    const INCREMENT = 'myfunc';
    
    const mutations = {
        [INCREMENT] (state) {
          state.count++;
        }
    }
    

    相當於上面的程式碼,結果是

    const mutations = {
        myfunc(state) {
          state.count++;
        }
    }

    const INCREMENT = 'myfunc';
    const mutations = {
        INCREMENT (state) {
          state.count++;
        }
    }

    的結果是

    const mutations = {
        INCREMENT(state) {
          state.count++;
        }
    }

    回覆
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:44:12

    這是 computed property names

    https://developer.mozilla.org...

    回覆
    0
  • 取消回覆