您的位置:首頁技術文章
文章詳情頁

javascript - js中括號問題

瀏覽:140日期:2022-12-14 17:16:02

問題描述

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
相關文章:
国产综合久久一区二区三区