onButtonClick只有一个参数时候,map和object对象都可以
// onButtonClick1(3) onButtonClick只有一个参数时候,map和object对象都可以
const map1 = new Map()
.set(1, { message: 'processing', page: 'indexPage' })
.set(2, { message: 'fail', page: 'failPage' })
.set(3, { message: 'success', page: 'successPage' })
.set(4, { message: 'cancel', page: 'cancelPage' })
.set('default', { message: 'other', page: 'otherPage' })
const onButtonClick2 = status => {
if (map1.has(status)) {
let { message, page } = map1.get(status)
sendLog(message)
jumpTo(page)
}
}
const sendLog = message => {
console.log(message);
}
const jumpTo = page => {
console.log(page);
}
增加onButtonClick参数 ,这个时候map是最好的选择
// 增加onButtonClick参数 ,这个时候map是最好的选择
const map3 = new Map()
.set({ identity: 'guest', status: 1 }, { message: 'processing', page: 'indexPage' })
.set({ identity: 'guest', status: 2 }, { message: 'fail', page: 'failPage' })
.set({ identity: 'guest', status: 3 }, { message: 'success', page: 'successPage' })
.set({ identity: 'guest', status: 4 }, { message: 'cancel', page: 'cancelPage' })
.set({ identity: 'guest', status: 5 }, { message: 'other', page: 'otherPage' })
.set({ identity: 'master', status: 1 }, { message: 'processing', page: 'indexPage' })
.set({ identity: 'master', status: 2 }, { message: 'fail', page: 'failPage' })
.set({ identity: 'master', status: 3 }, { message: 'success', page: 'successPage' })
.set({ identity: 'master', status: 4 }, { message: 'cancel', page: 'cancelPage' })
.set({ identity: 'master', status: 5 }, { message: 'other', page: 'otherPage' })
const onButtonClick4 = (identity, status) => {
const mapItem = [...map3].find(([key, value]) => {
return key.identity === identity && key.status === status
})
if (!mapItem) return
const [key, { message, page }] = mapItem
sendLog(message)
jumpTo(page)
}
onButtonClick4('master', 3)