2012-03-30 26 views
5

Tôi đang cố gắng để đạt được một mức độ thừa kế trong JavaScript và đây là những gì tôi có cho đến nay:Thừa kế JavaScript hoặc cách thức hoạt động này?

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

Nếu bạn không quen với khuôn khổ Vòng đấu loại, đó là OK, vì nó có thể là không thích hợp để này thảo luận. Tôi đã không nhìn thấy thừa kế thực hiện chính xác như thế này bất cứ nơi nào mà tôi đã nhìn. Vì nó hiện đang đứng, điều này hoạt động chính xác như thế nào bạn nghĩ rằng nó nên. Khi gọi clearDomestic(), phiên bản chính xác của hàm được gọi trong lớp được thừa kế và this trỏ đến đối tượng Company. Nếu tôi lấy ra số base và gọi tới số base(this), nó sẽ bị hỏng.

Có ai có thể giải thích lý do tại sao tính năng này hoạt động không? Nếu đây là một thực hành xấu, ai đó có thể cho tôi biết làm thế nào để viết lại nó để nó hoạt động giống nhau? Tôi không thực sự muốn bao gồm một thư viện khác để đạt được điều này.

CẬP NHẬT

Nếu bên Address bạn gọi this.clearDomestic() ngoài ko.computed, nó cố gắng gọi clearDomestic() gắn liền với Company nhưng sau đó this trỏ đến một đối tượng Address và do đó businessPhone không còn được xác định.

UPDATE 2

tôi đã chuyển những thứ xung quanh một lần nữa, và tôi đã giải quyết trên phương pháp này. Nó không phải lý tưởng, nhưng đó là cách duy nhất luôn hoạt động.

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

Tôi sẽ phải làm logic trong newstatenewcountry trong mỗi đối tượng được thừa kế từ Address mà làm này xa lý tưởng, nhưng cho đến khi tôi tìm thấy một gợi ý tốt hơn, tôi đang mắc kẹt với điều này .

+0

Hàm 'ko.observable()' đó phải trả về một hàm thực hiện phép thuật nào đó với 'this.base'. – Pointy

Trả lời

1

Tôi không chắc mình hiểu chính xác vấn đề, nhưng thay vì this.base(this);, hãy thử gọi this.base.call(this);? (trong phiên bản đầu tiên của mã của bạn).

Các vấn đề liên quan