先定义动作序列,然后用一个函数来执行动作序列 var actions = [{ type: 1, msg: '你好' }, { type: 2, msg: '我是老师Tom' }, { type: 3, msg: '你是谁' }, { type: 4, msg: '获取数据' }, { type: 5, msg: '欢迎你', value: true, conditions: [{ type: 3, msg: '你上几年级了' }, { type: 6, msg: '执行动作' }] } ] function doAction(action) { if (!action) { return } console.log(action.msg) if (action.type === 1) { } else if (action.type === 5) { if (action.value) { doAction(action.conditions[0]) } else { doAction(action.conditions[1]) } } } actions.forEach(doAction)
先定义动作序列,然后用一个函数来执行动作序列