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>
感謝您的解釋。現在它是如何工作的更清楚。 –