javascript - 为什么我的 ActionButton 基类被覆盖?

标签 javascript asp.net inheritance

我的“基类”似乎没有正确填充。为什么?

<script type="text/javascript">

    var exceptions = {
        NotImplementedException: function (message) {
            this.name = 'NotImplementedException';
            this.message = message || 'Property or method is not implemented.';
        }
    };
    exceptions.NotImplementedException.prototype = Error.prototype;

    function ActionButton() {
        this.execute = function () {
            throw new exceptions.NotImplementedException("Execute is not implemented.");
        };
        this.render = function (data) {
            throw new exceptions.NotImplementedException("Render is not implemented.");
        };
        this.$template = function () {
            throw new exceptions.NotImplementedException("$template is not implemented.");
        };
    };

    function ImageActionButton() {
        this.image = { url: '' };
    };
    function TextActionButton() {
        this.text = '';
    };
    function StandardActionButton() {
        this.text = '';
        this.image = { url: '' };
    };
    function MenuActionButton() {
        this.buttons = [];
    };

    ImageActionButton.prototype = new ActionButton();
    ImageActionButton.prototype.constructor = ImageActionButton;

    TextActionButton.prototype = new ActionButton();
    TextActionButton.prototype.constructor = TextActionButton;

    StandardActionButton.prototype = new ActionButton();
    StandardActionButton.prototype.constructor = StandardActionButton;

    MenuActionButton.prototype = new ActionButton();
    MenuActionButton.prototype.constructor = MenuActionButton;

    // This fails
    if (ImageActionButton.prototype != ActionButton) {
        alert("ImageActionButton prototype is not ActionButton!");
    }
    // This works
    if (ImageActionButton.prototype.constructor != ImageActionButton) {
        alert("ImageActionButton prototype.constructor is not ImageActionButton!");
    }
</script>

最佳答案

我想你会想使用instanceof而不是像你一样进行比较。

if (ImageActionButton instanceof ActionButton) {
    alert("ImageActionButton prototype is not ActionButton!");
}

关于javascript - 为什么我的 ActionButton 基类被覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016257/

相关文章:

javascript - TinyMCE 未加载

c# - Visual Studio 2015 ASP.NET 身份与 Web 窗体(不是 ASP.NET MVC)

javascript - Gridview Javascript 函数中的服务器标记问题

c# - unity3d 中用于继承类的 CustomPropertyDrawer

javascript - 使用 IP 地址而不是域名来抓取网络服务器的屏幕

javascript - 通过ajax和jsp将javascript变量保存到MySql数据库

java - 父类(super class)和子类参数

c# - 如何在继承链的中间插入类?

javascript - Android webview不显示Javascript Append

ASP.Net Core razor 页面处理程序成为一条包罗万象的路线