2017-02-09 12 views
0

私はJavaScriptの専門家ではなく、ここでどのように既存のオブジェクトを拡張できますか?Javascriptオブジェクトフィーチャーセットを拡張する

HTML:

<li data-brandtarget="Netherlands"><img src="images/netherlands.png" alt="Netherlands">Netherlands</li> 
<li data-brandtarget="Netherlands"><img src="images/netherlands.png" alt="Netherlands">Netherlands</li> 

JS:私は国を作るしようとしています

MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet(
    // Brand Fetures 
    new storeLocator.Feature('Aquafleur-YES', 'Aquafleur'), 
    new storeLocator.Feature('Aquafarm-YES', 'Aquafarm'), 
    new storeLocator.Feature('Bm_Web-YES', 'Blue Marine'), 
    // The below mentioned features I want to be added based on a loop 
    //new storeLocator.Feature('Naamlandnat-Netherlands', 'Netherlands'), 
    //new storeLocator.Feature('Naamlandnat-Great Britain', 'Great Britain'), 
    //new storeLocator.Feature('Naamlandnat-France', 'France'), 
    //new storeLocator.Feature('Naamlandnat-Germany', 'Germany') 
); 

はダイナミックます。だから私はすべてのリストをループして属性を取得しています。さて、どのように拡張して既存のオブジェクトに新しい機能を追加することができますか?以下は私のアプローチです。

jQuery('.country-options li').each(function(){ 

    var countryName = jQuery(this).attr('data-brandtarget'); 

    MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet(
     // Country Features 
     new storeLocator.Feature('Naamlandnat-'+ countryName, countryName) 
    );  

}); 

私は私が推測するので、それは間違っている知っている:私は、既存のオブジェクトを上書きするため、唯一の最後の機能は、オブジェクトにとどまる各ループで何度も何度も新しいオブジェクトを作成しています。

ここで既存のフィーチャセットを上書きせずにフィーチャを拡張する正しい方法は何ですか?

私も試してみました: newを削除し、ちょうどstoreLocator.FeatureSet(...);ループ内でそれを維持するが、うまくいきませんでしたで。

答えて

1

私が見つけたreferenceによると、既存のFeatureSetadd()を呼び出すことができます助けを

MedicareDataSource.prototype.FEATURES_ = new storeLocator.FeatureSet(); 

jQuery('.country-options li').each(function(){ 

    var countryName = jQuery(this).attr('data-brandtarget'); 

    MedicareDataSource.prototype.FEATURES_.add(
    new storeLocator.Feature('Naamlandnat-'+ countryName, countryName) 
);  

}); 
+0

感謝を。 –

関連する問題