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...
相關文章:
1. javascript - js setTimeout在雙重for循環中如何使用?2. 老師,請問我打開browsersync出現這個問題怎么解決啊?3. javascript - js 萬物皆對象的問題4. 在mac下出現了兩個docker環境5. node.js - JavaScript的一個不能理解的地方6. javascript - JS使用ele.style.backgoundImage = ’’ =’none’取消背景圖片都無效7. javascript - js 修改表格元素的,可以用DOM操作實現嗎?8. android - 類似這樣的recyclerview滑動效果9. javascript - js一個call和apply的問題?10. python - xpath提取網頁路徑沒問題,但是缺失內容?
