首页 > 解决方案 > 如何在不首先创建类的实例的情况下访问类的子类

问题描述

我创建了一些类构造函数,但在使用new关键字之前,我想知道尚未创建的类对象中的属性值。在这个小提琴中,如果我打电话SMALL,我可以看到我想要的属性(seconds),但我不知道如何提取值(10)。

我已经展示了我尝试使用 4 个innerHTML函数的 4 个技术,我还尝试了许多变体.this

<div id="clicker" onclick="readSmall()">
  CLICK
</div>
<div id="output1">

</div>
<div id="output2">

</div>
<div id="output3">

</div>
<div id="output4">

</div>
div {
  width: 500px;
  height: 70px;
  border: solid red 2px;
}

#clicker {
  height: 20px;
}
class BIG {
  constructor(cellLevel, cellNum, facingDirection) {
    this.cellNum = cellNum;
    this.cellLevel = cellLevel;
    this.facingDirection = facingDirection;
  }
}

class SMALL extends BIG {
  constructor(cellLevel, cellNum, facingDirection) {
    super(cellLevel, cellNum, facingDirection);
    this.assetName = "Small";
    this.seconds = 10;
  }
}

function readSmall() {
  document.getElementById("output1").innerHTML = SMALL;
  document.getElementById("output2").innerHTML = SMALL.seconds;
  document.getElementById("output3").innerHTML = SMALL[0];
  document.getElementById("output4").innerHTML = SMALL.this.seconds;
}

标签: javascript

解决方案


推荐阅读