2012-03-30 12 views
5

que estoy tratando de conseguir un grado de herencia en JavaScript y esto es lo que tengo hasta ahora:Herencia de JavaScript o ¿Cómo funciona?

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

Si no está familiarizado con el marco del golpe de gracia, que está bien, ya que es probable que no pertinentes a esta discusión. No he visto herencia hecha exactamente así en cualquier lugar que he buscado. Tal como está actualmente, funciona exactamente como crees que debería ser. Cuando se invoca clearDomestic(), se llama a la versión correcta de la función en la clase heredada y this apunta a un objeto Company. Si saco el base y llamo al base(this), se rompe.

¿Alguien puede explicar por qué esto está funcionando? Si esta es una mala práctica, ¿alguien me puede decir cómo reescribirla para que funcione de la misma manera? Realmente no quiero incluir otra biblioteca para lograr esto.

ACTUALIZACIÓN

Si dentro Address se invoca this.clearDomestic() fuera del ko.computed, que intenta llamar a la clearDomestic() unidos a Company pero luego this puntos a un objeto Address y así businessPhone ya no se define.

ACTUALIZACIÓN 2

He movido las cosas de nuevo, y me he decidido por este método. No es ideal, pero es la única forma que funciona de manera consistente.

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

voy a tener que hacer la lógica en el newstate y newcountry en cada objeto que hereda de Address que hace que esta lejos de ser ideal, pero hasta que encuentre una idea mejor, estoy atascado con este .

+0

Esa función 'ko.observable()' debe devolver una función que haga algún tipo de magia con 'this.base'. – Pointy

Respuesta

1

No estoy seguro de entender el problema exactamente, pero en lugar de this.base(this);, intente llamar al this.base.call(this);? (en la primera versión de tu código).