簡體   English   中英

擴展Javascript對象功能集

[英]extending Javascript object feature set

我不是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(...); 在循環中,但是沒有用。

根據我發現的參考 ,您可以在現有FeatureSet上調用add()

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)
  );    

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM