JavaScript 继承或它是如何工作的?

标签 javascript oop knockout.js

我正在尝试在 JavaScript 中实现一定程度的继承,这是我目前所做的:

function Address() {
    this.address1 = ko.observable();
    this.address2 = ko.observable();
    this.country = ko.observableSafe(null, new Country(-1, '', false));
    this.city = ko.observable('');
    this.state = ko.observable();
    this.province = ko.observable('');
    this.zipCode = ko.observable();

    this.countryLookupID = '';
    this.stateLookupID = '';

    ko.computed(function () {
        var newCountry = this.country();
        if (newCountry) {
            this.countryLookupID = newCountry.id.toString();
            if (newCountry.international) {
                this.clearDomestic();
            }
            else {
                this.clearInternational();
            }
        }
        else {
            this.countryLookupID = "";


    }, this);
    ko.computed(function () {
        var newState = this.state();
        if (newState) {
            this.stateLookupID = newState.id.toString();
        }
        else {
            this.stateLookupID = "";
        }
    }, this);
}
Address.prototype.clearDomestic = function () { return true; };
Address.prototype.clearInternational = function () { return true; };

function Company() {
    this.base = Address;
    this.base(this);
    this.legalEntityID = ko.observable(0);
    this.legalEntityName = ko.observable('');
    this.isFemaleOwned = ko.observable(false);
    this.isMinorityOwned = ko.observable(false);
    this.webAddress = ko.observable();

    this.businessPhone = ko.observable();
    this.faxNumber = ko.observable();

    this.locked = ko.observable(false);

}
Company.prototype.constructor = Address;
Company.prototype.clearDomestic = function () {
    this.businessPhone('');
    this.state(null);
    this.zipCode('');
};
Company.prototype.clearInternational = function () {
    this.province('');
};

如果您不熟悉 Knockout 框架,没关系,因为它可能与本次讨论无关。在我看过的任何地方,我都没有看到完全像这样完成继承。就目前而言,这完全符合您的想法。调用 clearDomestic() 时,将在继承类中调用函数的正确版本,并且 this 指向一个 Company 对象。如果我取出 base 并调用 base(this),它就会中断。

谁能解释为什么这是有效的?如果这是一种不好的做法,有人可以告诉我如何重写它以使其功能相同吗?我真的不想包含另一个库来实现这一点。

更新

如果在 Address 中调用 ko.computed 之外的 this.clearDomestic(),它会尝试调用 clearDomestic () 附加到 Companythis 指向一个 Address 对象,所以 businessPhone 不是更长的定义。

更新 2

我又改变了一些东西,我决定采用这种方法。这并不理想,但这是始终如一的唯一方法。

function Address() {
    this.address1 = ko.observable();
    this.address2 = ko.observable();
    this.country = ko.observableSafe(null, new Country(-1, '', false));
    this.city = ko.observable('');
    this.state = ko.observable();
    this.province = ko.observable('');
    this.zipCode = ko.observable();

    this.countryLookupID = '';
    this.stateLookupID = '';
}
Address.prototype.clearDomestic = function () { return true; };
Address.prototype.clearInternational = function () { };

function Company() {
    this.legalEntityID = ko.observable(0);
    this.legalEntityName = ko.observable('');
    this.isFemaleOwned = ko.observable(false);
    this.isMinorityOwned = ko.observable(false);
    this.webAddress = ko.observable();

    this.businessPhone = ko.observable();
    this.faxNumber = ko.observable();

    this.locked = ko.observable(false);

    ko.computed(function () {
        var newCountry = this.country();
        if (newCountry) {
            this.countryLookupID = newCountry.id.toString();
            if (newCountry.international) {
                this.clearDomestic();
            }
            else {
                this.clearInternational();
            }
        }
        else {
            this.countryLookupID = "";
        }

    }, this);
    ko.computed(function () {
        var newState = this.state();
        if (newState) {
            this.stateLookupID = newState.id.toString();
        }
        else {
            this.stateLookupID = "";
        }
    }, this);
}
Company.prototype = new Address;
Company.prototype.clearDomestic = function () {
    // Since we are entering this method via Address, we need a reference back to a real company object with self.
    this.businessPhone('');
    this.state(null);
    this.zipCode('');
};
Company.prototype.clearInternational = function () {
    this.province('');
};

我将不得不在每个继承自 Address 的对象中执行 newstatenewcountry 中的逻辑,这使得到目前为止从理想中,但直到我找到更好的建议,我坚持这个。

最佳答案

我不确定我是否完全理解问题所在,但不要使用 this.base(this);,而是尝试调用 this.base.call(this); ? (在您的代码的第一个版本中)。

关于JavaScript 继承或它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945499/

相关文章:

javascript - 类型错误 : Cannot read property 'node' of undefined

Javascript 将文件中的所有模块作为全局变量导入

javascript - 带有 ionic2 的 map javascript api 无法正常工作

javascript - 如何更改原型(prototype)值以覆盖对象值?是否可以?

javascript - 如何使用 Knockout JS 数组映射根据输入条件使选择选项绑定(bind)默认?

javascript - 无法使用 Knockout.js 获取未定义或 null 引用 API 的属性 'Id'

javascript - 无法与 Internet Explorer 一起使用的 Knockout 虚拟元素

javascript - 以 Angular 向 {{}} 添加占位符

c++ - 类具有相同的接口(interface),但参数类型不同

c++ - C++中的实例化