提问者:小点点

将对象名设置为变量的值


如何用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;
    }
}

共1个答案

匿名用户

如果我理解正确的话,这应该行得通

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

或者是速记,因为您已经定义了变量url

let obj = {
      url
};

相关问题