2012-03-30 19 views
5

मैं जावास्क्रिप्ट में उत्तराधिकार के एक डिग्री प्राप्त करने के लिए कोशिश कर रहा हूँ और यहाँ क्या मैं अब तक है:जावास्क्रिप्ट विरासत या यह कैसे काम कर रहा है?

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) पर कॉल करता हूं, तो यह टूट जाता है।

क्या कोई यह समझा सकता है कि यह क्यों काम कर रहा है? यदि यह एक बुरा अभ्यास है, तो क्या कोई मुझे बता सकता है कि इसे कैसे लिखना है ताकि यह वही कार्य करे? मैं वास्तव में इसे प्राप्त करने के लिए एक और पुस्तकालय शामिल नहीं करना चाहता हूं।

अद्यतन

आप ko.computed की this.clearDomestic() बाहर आह्वान Address अंदर हैं, तो इसे कहते हैं clearDomestic() एक Address वस्तु को this अंक Company से जुड़ी है, लेकिन फिर और इतने 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(''); 
}; 

मैं newstate और हर वस्तु है कि जो इस आदर्श से दूर करता है Address से विरासत में newcountry में तर्क क्या करना है करने के लिए जा रहा हूँ, लेकिन जब तक मैं एक बेहतर सुझाव मिल जाए, मैं इस के साथ अटक कर रहा हूँ ।

+0

कि 'ko.observable()' फ़ंक्शन को एक फ़ंक्शन वापस करना होगा जो 'this.base' के साथ किसी प्रकार का जादू करता है। – Pointy

उत्तर

1

मुझे यकीन नहीं है कि मैं वास्तव में समस्या को समझता हूं, लेकिन this.base(this); के बजाय, this.base.call(this); पर कॉल करने का प्रयास करें? (आपके कोड के पहले संस्करण में)।

संबंधित मुद्दे