javascript - js中括號問題
問題描述
import {INCREMENT} from './types'const mutations = { [INCREMENT] (state) { state.count++; }}
[INCREMENT] INCREMENT是變量直接使用不就行了嗎,為什么還要加一個中括號呢?
問題解答
回答1:[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++; }}回答2:
這是 computed property names
https://developer.mozilla.org...
相關文章:
