javascript - 如何在不首先创建类的实例的情况下访问类的子类
问题描述
我创建了一些类构造函数,但在使用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;
}
解决方案
推荐阅读
- go - 获取资源 SKU 以在 GCP 中查找资源价格 (go)
- sql - SQL如何同步聚合表
- python - 获取合并两个数据集的数据集格式
- python - 不确定这里出了什么问题,无论我尝试什么,它都会返回 TypeError: 'NoneType' object is not iterable
- oracle - 数字的 Oracle 字节大小
- r - 在 R Lubridate 中解析我的日期时间时出现问题
- php - 如何使用 cakephp 2 在视图中显示树视图
- selenium - 复选框中的 Xpath 定位器
- vue.js - 无法对 Vuesax 数据表中的日期列进行排序
- c++ - 关于抽象类(cpp)的运算符重载