2012-03-30 4 views
5

Ich versuche, einen Grad der Vererbung in JavaScript zu erreichen, und hier ist das, was ich bisher habe:JavaScript-Vererbung oder wie funktioniert das?

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

Wenn Sie mit dem Knockout Rahmen nicht vertraut sind, das ist in Ordnung, wie es dies wahrscheinlich nicht relevant ist Diskussion. Ich habe die Erbschaft nicht genau so gesehen, wo ich hingesehen habe. Wie es derzeit aussieht, funktioniert das genau so, wie Sie es für richtig halten. Wenn clearDomestic() aufgerufen wird, wird die richtige Version der Funktion in der geerbten Klasse und this Punkte auf ein Company Objekt aufgerufen. Wenn ich das base heraushole und base(this) rufe, bricht es.

Kann jemand erklären, warum das funktioniert? Wenn das eine schlechte Übung ist, kann mir jemand sagen, wie ich es umschreiben soll, damit es genauso funktioniert? Ich möchte nicht wirklich eine andere Bibliothek hinzufügen, um dies zu erreichen.

UPDATE

Wenn innerhalb Address Sie this.clearDomestic() außerhalb des ko.computed aufrufen, versucht er, die clearDomestic() an Company aber dann this Punkte auf ein Address Objekt aufzurufen und so ist businessPhone nicht mehr definiert.

UPDATE 2

ich Dinge um wieder verschoben haben, und ich habe in diesem Verfahren angesiedelt. Es ist nicht ideal, aber es ist der einzige Weg, der konsequent funktioniert.

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

Ich werde die Logik in der newstate und newcountry in jedem Objekt zu tun haben, die von Address erbt, welche diese bei weitem nicht ideal macht, aber bis ich einen besseren Vorschlag finden, ich bin mit diesem stecken .

+0

Die 'ko.observable()' Funktion muss eine Funktion zurückgeben, die eine Art Magie mit 'this.base' macht. – Pointy

Antwort

1

Ich bin mir nicht sicher, ob ich das Problem genau verstehe, aber anstatt this.base(this);, versuchen Sie this.base.call(this); anrufen? (in der ersten Version Ihres Codes).