gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 04:38:32 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com