javascript - js中括號(hào)問(wèn)題
問(wèn)題描述
import {INCREMENT} from './types'const mutations = { [INCREMENT] (state) { state.count++; }}
[INCREMENT] INCREMENT是變量直接使用不就行了嗎,為什么還要加一個(gè)中括號(hào)呢?
問(wèn)題解答
回答1:[INCREMENT]是計(jì)算INCREMENT這個(gè)變量的值作為函數(shù)名,不使用中括號(hào)是把INCREMENT這個(gè)字符串作為函數(shù)名。
const INCREMENT = ’myfunc’;const mutations = { [INCREMENT] (state) { state.count++; }}
相當(dāng)于上面的代碼,結(jié)果是
const mutations = { myfunc(state) { state.count++; }}
而
const INCREMENT = ’myfunc’;const mutations = { INCREMENT (state) { state.count++; }}
的結(jié)果是
const mutations = { INCREMENT(state) { state.count++; }}回答2:
這是 computed property names
https://developer.mozilla.org...
相關(guān)文章:
1. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?2. python沒(méi)入門,請(qǐng)教一個(gè)問(wèn)題3. 小程序怎么加外鏈,語(yǔ)句怎么寫!求救新手,開(kāi)文檔沒(méi)發(fā)現(xiàn)4. php如何獲取訪問(wèn)者路由器的mac地址5. html - 爬蟲時(shí)出現(xiàn)“DNS lookup failed”,打開(kāi)網(wǎng)頁(yè)卻沒(méi)問(wèn)題,這是什么情況?6. javascript - 我的站點(diǎn)貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?7. 求教一個(gè)mysql建表分組索引問(wèn)題8. node.js - 用nodejs 的node-xlsx模塊去讀取excel中的數(shù)據(jù),可是讀取出來(lái)的日期是數(shù)字,請(qǐng)問(wèn)該如何讀取日期呢?9. html5 - input type=’file’ 上傳獲取的fileList對(duì)象怎么存儲(chǔ)于瀏覽器?10. javascript - 在 vue里面用import引入js文件,結(jié)果為undefined
