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...
相关文章:
