如何解决为什么我的子类ES5 OOP没有在单击按钮时运行?
问题是当我单击 id 为 squareBtn 的按钮时,它没有运行 Square 类。我选择了以相同方式创建形状类的按钮,它工作得很好。我在我的孩子 square 课上有一个警报,它甚至没有运行。我知道这可能很愚蠢,但我找不到解决方案,所以我来这里寻求帮助,所以请怜悯。
class Shape {
constructor() {
this.div = $('<div class="position-absolute"></div>');
this.positioning();
$(this.div).css('background-color',allHSLAcolors[randomNumber(0,360)]);
$(this.div).dblclick(() => this.div.remove());
$('#container').append(this.div);
}
positioning() {
$(this.div).css('top',randomNumber(0,400) + "px");
$(this.div).css('left',100) + "%");
}
}
class Square extends Shape {
constructor() {
this.length = $('#square').val();
$(this.div).css('height',this.length + 'px');
$(this.div).css('width',this.length + 'px');
alert('hi');
}
}
$('#hey').on('click',() => new Shape);
$('#squareBtn').on('click',() => new Square);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="d-flex justify-content-center">
<button id="squareBtn" class="btn mx-1 btn-primary" for="square">Square</button>
<input id="square" type="number" placeholder="Length">
</div>
解决方法
- 就是
new Square
。
$('#squareBtn').on('click',() => new Square);
- 您需要在构造函数中调用
super
。
class Square extends Shape {
constructor() {
super();
// ...
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。