我試圖實現一定程度的JavaScript中繼承和這裏是我到目前爲止有: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('');
};
如果你不熟悉的淘汰賽框架,這是確定的,因爲它可能不屬於本討論。我還沒有看到完全像我看過的任何地方的繼承。就目前來看,這種方式正是你認爲應該如此的方式。調用clearDomestic()
時,在繼承的類中調用正確的函數版本,this
指向Company
對象。如果我拿出base
並致電base(this)
,它會中斷。
任何人都可以解釋爲什麼這是工作?如果這是一個不好的做法,有人可以告訴我如何重寫它,使其功能相同嗎?我不想包含另一個庫來實現這一點。
UPDATE
如果裏面Address
調用this.clearDomestic()
外ko.computed
的,它試圖調用附clearDomestic()
到Company
但隨後this
指向一個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
繼承這使得這個很不理想中的每個對象的newstate
和newcountry
邏輯,但直到我找到一個更好的建議,我堅持這個。
'ko.observable()'函數必須返回一個函數,該函數通過'this.base'完成某種魔術。 – Pointy 2012-03-30 15:05:17