As a newcomer to Javascript and vue.js, I recently started using Vue to create a management system for our company. While studying codes online to learn how to use it efficiently, I came across a code fragment that puzzled me. How can I define a function in square brackets?
This code is specifically used for vuex.
//mutation-types.js
export const ADD_TOTAL_TIME = 'ADD_TOTAL_TIME'
export const DEC_TOTAL_TIME = 'DEC_TOTAL_TIME'
export const SAVE_PLAN = 'SAVE_PLAN'
export const DELETE_PLAN = 'DELETE_PLAN'
//mutation.js
import * as types from './mutation-types'
export default {
[types.ADD_TOTAL_TIME] (state, time) {
state.totalTime = state.totalTime + time
},
[types.DEC_TOTAL_TIME] (state, time) {
state.totalTime = state.totalTime - time
},
[types.SAVE_PLAN] (state, plan) {
const avatar = 'https://sfault-avatar.b0.upaiyun.com/147/223/147223148-573297d0913c5_huge256'
state.list.push(
Object.assign({name: 'Jack', avatar: avatar}, plan)
)
},
[types.DELETE_PLAN] (state, idx) {
state.list.splice(idx, 1)
}
}