2012-03-30 31 views
5

我試圖實現一定程度的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繼承這使得這個很不理想中的每個對象的newstatenewcountry邏輯,但直到我找到一個更好的建議,我堅持這個。

+0

'ko.observable()'函數必須返回一個函數,該函數通過'this.base'完成某種魔術。 – Pointy 2012-03-30 15:05:17

回答

1

我不確定我是否完全瞭解問題,但不是this.base(this);,請嘗試撥打this.base.call(this);? (在您的代碼的第一個版本中)。