如何从构造函数JavaScript复制“ this”

时间:2019-11-23 12:34:29

标签: javascript class closures

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

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

1 个答案:

答案 0 :(得分:0)

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

尝试一下:

this.anotherCard = _.cloneDeep(card);