我有一个js文件,如下所示。正如您所看到的所有函数具有相同的语法,只有不同的是实际的实现。我有一种感觉,我可以编写一些声明实际功能的包装函数,然后我可以使用其他命令函数而不会一次又一次地重复语法。但是我无法理解我应该怎么做呢!
'use strict'
module.exports = () => {
const firstName = (name) => name.split(' ', 1)[0]
const commands = {
s (cb, name, text, session) { cb(null, 'ee') }
}
// commands.execute = (cb, name, text, session)
commands.salut = (cb, name, text, session) => {
session.firstName = firstName(name)
cb(null, `Hi ${session.firstName}`)
}
commands.bye = (cb, name, text, session) =>
cb(null, `bye ${session.firstName}`)
commands.help = (cb, name, text, session) =>
cb(null, `help me too`)
commands.noMapping = (cb, name, text, session) =>
cb(null, `sorry I haven't been programmed for this cmd, to end the chat type 'bye bot'`)
return commands
}
欢迎任何指示!