2012-03-30 1 views
5

私はJavaScriptでの継承の度合いを達成しようと、ここで私がこれまで持っているものですよ:JavaScriptの継承またはこれはどのように機能していますか?

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

あなたはノックアウトフレームワークに慣れていない場合、それはおそらく、これに関連はないとして、それは、OKです討論。私は見たところのどこでもこれと全く同じように継承が行われているのを見ていません。現在のところ、これはあなたがそれをどう考えるべきか正確に機能します。 clearDomestic()が呼び出されると、関数の正しいバージョンが継承されたクラスで呼び出され、thisCompanyオブジェクトを指しています。 baseを取り出してbase(this)に電話すると、それが破損します。

誰でもこれがなぜ機能しているのか説明できますか?これが悪い習慣であれば、誰かが私にそれを書き直す方法を教えて、それが同じように機能するようにすることができますか?私は本当にこれを達成するための別の図書館を含めたくはありません。

UPDATE

あなたはko.computedthis.clearDomestic()外を呼び出すAddress内部の場合、それはCompanyに添付clearDomestic()を呼び出していないが、その後thisポイントAddress対象にし、そうbusinessPhoneはもはや定義されてしようとします。

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

私はこの理想からかけ離れますAddressから継承するすべてのオブジェクトでnewstatenewcountryでロジックを実行するために持っているつもりですが、私はより良い提案を見つけるまで、私はこれにこだわっています。

+0

'ko.observable()'関数は 'this.base'で何らかの魔法を行う関数を返す必要があります。 – Pointy

答えて

1

私は問題を正確に理解できませんが、this.base(this);の代わりにthis.base.call(this);を呼び出してみてください。 (コードの最初のバージョンで)。

関連する問題