【发布时间】:2012-08-29 09:47:05
【问题描述】:
我正在尝试理解 Javascript OOP。我正在尝试覆盖类中的方法。当“点击”时,该类具有默认功能。我想覆盖那个函数,所以当点击时会发生一些新的事情。
我有一个如下所示的 Javascript 类:
AlertModal = function(){
var x = *this is my close object;
x.onclick = destoryAlert;
function destroyAlert(){
console.log('destroy');
}
}
我的 HTML 文件显示:
<script type="text/javascript">
window.alert = function (message) {
var newAlert = new AlertModal();
newAlert.destroyAlert = function(){
console.log('new alert destroy');
};
newAlert.destroyAlert();
};
我收到“新警报销毁”,这很棒。但是当我单击“x”时,它也表示销毁。所以它被覆盖了,但不是?!就像它在调用时创建了一个新的“destroyAlert”函数,但保留了默认值。
谁能告诉我如何做到这一点,创建一个具有默认功能的类,但如果需要如何覆盖它?
我习惯于使用 Java 和 Actionscript 进行编程、扩展类和覆盖公共/受保护的方法,但这样做 Javascript 似乎有很大不同,我无法理解这样做的逻辑。
谢谢,
【问题讨论】:
标签: javascript class overriding overwrite