Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript OOP-为什么参数对象在实例方法中不可见?_Javascript_Jquery_Oop - Fatal编程技术网

javascript OOP-为什么参数对象在实例方法中不可见?

javascript OOP-为什么参数对象在实例方法中不可见?,javascript,jquery,oop,Javascript,Jquery,Oop,我创建了jQuery对话框类 基类是AlertDialog。子类是ChildAlertDialog 当ChildAlertDialog被实例化时,它接受elementId(DOM id)和executor对象(MyExcecutor实例),并使用close按钮显示看起来像警报的对话框 Close按钮触发MyExecutor的实例方法executeClose() 但当我打电话时: alertbox.displayDialog(); 我得到一个错误: executor is undefined e

我创建了jQuery对话框类

基类是AlertDialog。子类是ChildAlertDialog

当ChildAlertDialog被实例化时,它接受elementId(DOM id)和executor对象(MyExcecutor实例),并使用close按钮显示看起来像警报的对话框

Close按钮触发MyExecutor的实例方法
executeClose()

但当我打电话时:

alertbox.displayDialog();
我得到一个错误:

executor is undefined
executor.executeClose();
如果我将executor传递给displayDialog()方法,它就会工作。所以我有点困惑,因为它可以在其他语言如Java中工作,但javascript似乎不同。我是否应该将executor传递给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
构造函数时,
executor
只能在
ChildAlertDialog
中访问

要确保执行器可访问
displaydalog
,请显式调用
AlertDialog

function ChildAlertDialog(elementId, executor) {
    AlertDialog.call(this, elementId, executor);
    this.height = 70;
    this.width = 400;
}
或者将
executor
作为对话框对象的属性

function AlertDialog(elementId, executor) {
    this.executor = executor;
    ...

    this.displayDialog = function() {
        ...
                    this.executor.executeClose();
}

//child class
function ChildAlertDialog(elementId, executor) {
    this.executor = executor;
    ...
}
Douglas Crockford正是因为这个问题,即两者之间缺乏分离,才开发了一个浏览器原生JS函数

function AlertDialog(elementId, executor) {
    this.elementId = elementId;
    this.executor = executor;
}
AlertDialog.prototype.height = null;
AlertDialog.prototype.width = null;
AlertDialog.prototype.displayDialog = function() {
    $(this.elementId).dialog({
        autoOpen: true,
        height: this.height,
        width: this.width,
        buttons: {
            'close me': function() {
                this.executor.executeClose();
            }
        }
    });
};


//child class
function ChildAlertDialog(elementId, executor) {
    AlertDialog.call(this, elementId, executor);
    this.height = 70;
    this.width = 400;
}

//inheritance
ChildAlertDialog.prototype = Object.create(AlertDialog.prototype, 
        {constructor: {value: ChildAlertDialog, writable: false, enumerable: false}});

函数参数的作用域仅限于该函数。在对象创建过程中调用
ChildAlertDialog
构造函数时,
executor
只能在
ChildAlertDialog
中访问

要确保执行器可访问
displaydalog
,请显式调用
AlertDialog

function ChildAlertDialog(elementId, executor) {
    AlertDialog.call(this, elementId, executor);
    this.height = 70;
    this.width = 400;
}
或者将
executor
作为对话框对象的属性

function AlertDialog(elementId, executor) {
    this.executor = executor;
    ...

    this.displayDialog = function() {
        ...
                    this.executor.executeClose();
}

//child class
function ChildAlertDialog(elementId, executor) {
    this.executor = executor;
    ...
}
Douglas Crockford正是因为这个问题,即两者之间缺乏分离,才开发了一个浏览器原生JS函数

function AlertDialog(elementId, executor) {
    this.elementId = elementId;
    this.executor = executor;
}
AlertDialog.prototype.height = null;
AlertDialog.prototype.width = null;
AlertDialog.prototype.displayDialog = function() {
    $(this.elementId).dialog({
        autoOpen: true,
        height: this.height,
        width: this.width,
        buttons: {
            'close me': function() {
                this.executor.executeClose();
            }
        }
    });
};


//child class
function ChildAlertDialog(elementId, executor) {
    AlertDialog.call(this, elementId, executor);
    this.height = 70;
    this.width = 400;
}

//inheritance
ChildAlertDialog.prototype = Object.create(AlertDialog.prototype, 
        {constructor: {value: ChildAlertDialog, writable: false, enumerable: false}});

您重写构造函数,并且从不调用设置此函数的原始版本。在实例范围内绑定执行器的第一个构造函数从未实际运行。所以你必须调用它,语法有点奇怪

请尝试此操作,它调用原始版本:

//child class
function ChildAlertDialog(elementId, executor) {
    // Run the parent constructor function
    AlertDialog.call(this, elementId, executor);

    // Child class setup.
    this.elementId = elementId;
    this.height = 70;
    this.width = 400;
}

//inheritance
ChildAlertDialog.prototype = new AlertDialog();
ChildAlertDialog.prototype.constructor = ChildAlertDialog;

为了在实例上运行构造函数,必须使用
ConstructorFunction.call(this,args…
)。遗憾的是,原型和构造函数以非常不同的方式继承。这就是为什么在构造函数中设置实例方法通常不是一个好主意。

您重写构造函数函数,并且从不调用设置该函数的原始版本。在实例范围内绑定执行器的第一个构造函数从未实际运行。所以你必须调用它,语法有点奇怪

请尝试此操作,它调用原始版本:

//child class
function ChildAlertDialog(elementId, executor) {
    // Run the parent constructor function
    AlertDialog.call(this, elementId, executor);

    // Child class setup.
    this.elementId = elementId;
    this.height = 70;
    this.width = 400;
}

//inheritance
ChildAlertDialog.prototype = new AlertDialog();
ChildAlertDialog.prototype.constructor = ChildAlertDialog;

为了在实例上运行构造函数,必须使用
ConstructorFunction.call(this,args…
)。遗憾的是,原型和构造函数以非常不同的方式继承。这就是为什么在构造函数中设置实例方法通常不是一个好主意。

将执行器传递给ChildAlertDialog,但从未在AlertDialog上设置。但我不想为我的子类重新定义displayDialog()方法。将执行器传递给ChildAlertDialog,但它从未设置在AlertDialog上,但我不想为我的子类重新定义displayDialog()方法。