简体   繁体   English

如何覆盖面向对象的Javascript中的方法?

[英]How to override a method in Object Oriented Javascript?

I was trying to implement an interface like architecture in JS as followed in C# . 我试图实现一个interface在这样的架构JS为遵循C# And met with a stumbling block. 并遇到了绊脚石。 Here is the code sample: 这是代码示例:

    // Interface for UIBuilder classes
    function IUIBuilder() {
        this.addUserToList = function () {
            alert('parent: added');
        };
    }

    // Class implementing the IUIBuilder
    function ChatUIBuider() {
        IUIBuilder.prototype.addUserToList = function () {
            alert('child: added');
        };
        IUIBuilder.prototype.removeUserFromList = function () {
            alert('child: removed');
        };

        return new IUIBuilder();
    }

In the first class, I've defined a method addUserToList which I override in the second class ChatUIBuider . 在第一个类中,我定义了一个方法addUserToList ,我在第二个类ChatUIBuider覆盖ChatUIBuider Also added one more method removeUserFromList to the base class using its prototype. 还使用其原型将另一个方法removeUserFromList添加到基类。

My issue is, the addUserToList method still invokes the parent class method even after it has got overridden in the child class. 我的问题是, addUserToList方法仍然会调用父类方法,即使它已在子类中被重写。 Why? 为什么?

    var builder = new ChatUIBuider();
    builder.removeUserFromList(); // Invokes the child class method. - CORRECT
    builder.addUserToList(); // Invokes the base class method- WHY??

Could anyone tell me if this is the correct way I am doing? 谁能告诉我这是否是我正在做的正确方法?

I suggest this construct : 我建议这个结构:

function IUIBuilder() {
};
IUIBuilder.prototype.addUserToList = function () {
    alert('parent: added');
};

// Class extending the IUIBuilder
function ChatUIBuider() {
}
ChatUIBuider.prototype = new IUIBuilder();
ChatUIBuider.prototype.addUserToList = function () {
        alert('child: added');
};
ChatUIBuider.prototype.removeUserFromList = function () {
        alert('child: removed');
};

ChatUIBuider extends IUIBuilder and inherits its functions but overrides the addUserToList function. ChatUIBuider扩展了IUIBuilder并继承了它的功能,但是覆盖了addUserToList函数。

In the following code, both constructors will be called but only the overriding addUserToList function will be called : 在下面的代码中,将调用两个构造函数,但只会调用重写的addUserToList函数:

var chat = new ChatUIBuider();
chat.addUserToList();

See demonstration 示范

@Denys restructured the entire code , without exactly pointing out the issue. @Denys重组了整个代码,没有完全指出问题。 issue is addUserToList is not a prototype method of your parent class , it's a this method which is copied for every instance and not sahred. 问题是addUserToList不是你的父类的原型方法,它的this方法被复制的每一个实例,而不是sahred。 So just converting it to a prototype method fixes the issue. 因此,只需将其转换为prototype方法即可解决问题。

 // Interface for UIBuilder classes
    function IUIBuilder() {
    }
    IUIBuilder.prototype.addUserToList = function () {
            alert('parent: added');
    };

    // Class implementing the IUIBuilder
    function ChatUIBuider() {
        IUIBuilder.prototype.addUserToList = function () {
            alert('child: added');
        };
        IUIBuilder.prototype.removeUserFromList = function () {
            alert('child: removed');
        };

        return new IUIBuilder();
    }
    var builder = new ChatUIBuider();
    builder.removeUserFromList(); // Invokes the child class method. - CORRECT
    builder.addUserToList(); // Invokes the CHILD CLASS's METHOD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM