我創建了jQuery對話框類。JavaScript的OOP - 如何參數對象不可見實例方法?
基類是AlertDialog。子類是ChildAlertDialog。
當ChildAlertDialog被實例化時,它需要elementId(DOM id)和executor對象(MyExcecutor實例),並用關閉按鈕顯示alert alert對話框。
關閉按鈕觸發MyExecutor的實例方法executeClose();
但是,當我打電話:
alertbox.displayDialog();
我得到一個錯誤:
executor is undefined
executor.executeClose();
它的工作原理,如果我通過執行人displayDialog()方法。所以我有點困惑,因爲它可以像Java這樣的其他語言工作,但JavaScript似乎不同。我應該只是通過執行器displayDialog()?或者有更好的方法嗎?
------對話框類------------
//base class
function AlertDialog(elementId, executor) {
this.elementId = elementId;
this.height = null;
this.width = null;
this.displayDialog = function() {
$(this.elementId).dialog({
autoOpen: true,
height: this.height,
width: this.width,
buttons: {
'close me': function() {
executor.executeClose(); //ERROR HERE!!!!!
}
}
});
}
//child class
function ChildAlertDialog(elementId, executor) {
this.elementId = elementId;
this.height = 70;
this.width = 400;
}
//inheritance
ChildAlertDialog.prototype = new AlertDialog();
ChildAlertDialog.prototype.constructor = ChildAlertDialog;
使用ChildAlertDialog類的類。
function MyExcecutor() {
this.execute = function() {
//passing elementID and MyExecutor object to ChildAlertDialog
var alertbox = new ChildAlertDialog("#someDiv",this);
alertbox.displayDialog();
}
this.executeClose = function() {
//do something when the dialog is closed
}
}
您將一個執行器傳遞給ChildAlertDialog,但它從未在AlertDialog上設置。 –
但我不想爲我的子類重新定義displayDialog()方法。 –