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. macos - 如何徹底刪除mac自帶的apache和php2. javascript - sublime快鍵鍵問(wèn)題3. docker 17.03 怎么配置 registry mirror ?4. DADB.class.php文件的代碼怎么寫(xiě)5. javascript - immutable配合react提升性能?6. css - 寫(xiě)頁(yè)面遇到個(gè)布局問(wèn)題,求大佬們幫解答,在線等,急!~7. mysql事務(wù)回滾定位8. javascript - html5多個(gè)label中其中一個(gè)觸發(fā)change,如何判斷是哪一個(gè)出發(fā)了change9. 實(shí)現(xiàn)bing搜索工具urlAPI提交10. css - 移動(dòng)端字體設(shè)置問(wèn)題

網(wǎng)公網(wǎng)安備