【问题标题】:How to copy "this" from constructor JavaScript如何从构造函数 JavaScript 复制“this”
【发布时间】:2020-03-19 07:38:30
【问题描述】:

我正在尝试从constructor 复制this.card。但是,到目前为止它不起作用。也许这是不可能的。无论如何,有人可以告诉我该怎么做吗?

class CardList {
  constructor(card) {
    this.card = card;
  }

  render(container, cards) {
    this.container = container;
    this.cards = cards;

    this.cards.forEach(card => {
      this.card.placeImage.setAttribute(
        "style",
        `background-image: url(${card.link})`
      );

      this.card.placeName.textContent = card.name;
      this.container.appendChild(this.card.placeCard);
    });
  }
}

【问题讨论】:

    标签: javascript class closures


    【解决方案1】:

    您可以使用lodash 轻松地深度克隆对象。

    试试这个:

    this.anotherCard = _.cloneDeep(card);
    

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 1970-01-01
      • 2019-08-07
      • 2018-01-16
      • 2016-02-07
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多