【问题标题】:set the object name to the value of a variable将对象名称设置为变量的值
【发布时间】:2020-06-28 20:27:34
【问题描述】:

如何用 url 的值命名对象?

let url = 'https://example.com/'
const obj = new constr(img) //i want to name this object (obj) with the ulr's string

class constr(img){
    this.img = img;
    }
}

【问题讨论】:

  • “命名这个对象”是什么意思?
  • @Quentin 我想创建一个名为 example.com 的对象
  • 你说的“名字”是什么意思?对象通常没有名称(函数是主要的例外),所以它真的不清楚你要做什么。
  • 我想创建一个像 example.com{ prop1:'....' } 的对象
  • JSON 中的对象与您在此问题中提出的问题无关(也没有名称作为功能)。

标签: javascript node.js


【解决方案1】:

如果我理解正确,这应该可以工作

let obj = {
  url: 'https://example.com/'
};

或者简写,因为你已经定义了变量url

let obj = {
      url
};

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多