0

我在 JS 中有一段代码:

var commands = require('commands.json');
module.exports = class CatCommand extends commando.Command {
    constructor(client) {
        super(client, {
            name: 'cat',
            group: 'fun',
            aliases: commands[this.group][this.name].aliases || [utils.translate(this.name)],
            memberName: name,
            description: commands[this.group][this.name].description || 'No description',
            examples: commands[this.group][this.name].usages || ['No examples'],
            throttling: {
                usages: 2,
                duration: 5
            }
        });
    }

    async run(message) {}
}

我需要使用变量namegroup作为commands'键。最好的方法是什么?

4

1 回答 1

0

由于您正在构建该对象,因此您无法获得这些属性(它们在技术上尚不存在)。
尝试在构造函数之外的变量中声明这些值,如下所示:

var name = "cat",
  group = "fun";

module.exports = class CatCommand extends commando.Command {
  constructor(client) {
    super(client, {
      name,
      group,
      aliases: commands[group][name].aliases || [utils.translate(name)],
      memberName: name,
      description: commands[group][name].description || 'No description',
      examples: commands[group][name].usages || ['No examples'],
      throttling: {
        usages: 2,
        duration: 5
      }
    });
  }
}
于 2018-12-26T18:33:45.527 回答