0
我很难理解JavaScript中如何覆盖方法。Javascript继承方法覆盖不起作用
在下面的代码,我有CustomFieldTable类的子类从表类,并将其两者具有createList功能。
如何从下面的代码重写createList功能,以便从CustomFieldTable类createList功能可以从重装功能运行?
当前控制台输出:
'Should be silent overridden createList Function'
期望中的控制台输出:
'Custom Field create list'
'obj1'
'obj2'
'obj3'
$(document).ready(function() {
var table = new CustomFieldTable();
table.init();
});
function Table() {
var self = this;
self.table_data = [];
self.reload = function() {
self.table_data = ["obj1", "obj2", "obj3"];
self.createList();
}
self.createList = function() {
alert("Should be silent overridden createList Function");
}
}
CustomFieldTable.prototype = new Table();
CustomFieldTable.prototype.constructor = CustomFieldTable;
function CustomFieldTable() {
var self = this;
self.init = function() {
self.reload();
}
self.createList = function() {
alert("Custom Field create list");
for (var i = 0; i < self.table_data.length; i++) {
alert(self.table_data[i]);
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
感谢您的解释。现在它是如何工作的更清楚。 –