您的位置:首页技术文章
文章详情页

javascript - js中括号问题

【字号: 日期:2022-12-14 17:16:02浏览:90作者:猪猪

问题描述

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...

标签: JavaScript
相关文章: