2012-03-30 32 views
5

Burada JavaScript miras derecesi elde etmeye çalışıyor ve yaşıyorum ben bugüne kadar ne var, ne belki buna uygun değil gibiJavaScript Kalıtım veya Bu Nasıl Çalışır?

Eğer Nakavt çerçevesi haberiniz yoksa
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(''); 
}; 

, yani sorun yok tartışma. Gördüğüm her yerde tam olarak böyle miras görmedim. Halen durduğu gibi, bu tam olarak nasıl gerektiğini düşündüğünüz gibi çalışır. clearDomestic() çağrıldığında, işlevin doğru sürümü, devralınan sınıfta ve Company nesnesine this noktasında çağrılır. base'u çıkarırsam ve base(this)'u arayarak kırılır.

Bunun neden çalıştığını açıklayan herkes var mı? Bu kötü bir uygulamasa, birisi bana nasıl yazılacağını söyler, böylece aynı şekilde çalışır? Bunu başarmak için başka bir kütüphane eklemek istemiyorum.

GÜNCELLEME

Eğer ko.computed ait this.clearDomestic() dışında çağırmak Address içeride ise, clearDomestic()Company bağlı ama sonra this puan bir Address nesneye ve böylece businessPhone artık tanımlanır aramayı dener.

GÜNCELLEME 2

Etrafa tekrar şeyler hareket ettik ve bu yöntemle oturttuktan. İdeal değil, ama sürekli çalışan tek yol.

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(''); 
}; 

Ben idealden uzak bu yapar Address devraldığı her nesnede newstate ve newcountry mantığı yapmak zorunda kalacağım, ama daha iyi bir öneri bulana kadar, bu takıldım .

+0

Bu 'ko.observable() işlevi' this.base' ile bir çeşit sihir yapan bir işlevi döndürmelidir. – Pointy

cevap

1

Sorunu tam olarak anladığımdan emin değilim, ancak this.base(this); yerine, this.base.call(this); numaralı telefonu arayabilir misiniz? (kodunuzun ilk versiyonunda).

İlgili konular