【问题标题】:How to get values from object without any "name"?如何从没有任何“名称”的对象中获取值?
【发布时间】:2019-05-24 19:28:50
【问题描述】:

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

【问题讨论】:

  • 无法引用“建设中”对象字面量的字段。如果您需要使用这些值,请将它们设为单独声明的变量的值。
  • 好的,我试试。谢谢!

标签: javascript node.js discord.js commando


【解决方案1】:

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

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
      }
    });
  }
}

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 2016-09-03
    相关资源
    最近更新 更多