如何从没有任何“名称”的对象获取值?

时间:2018-12-26 14:58:28

标签: javascript node.js discord.js commando

我在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'键。最好的方法是什么?

1 个答案:

答案 0 :(得分: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
      }
    });
  }
}