2016-07-25 92 views
2

我有一個地圖區域的對象。因爲我也爲所有其他領域使用主頁區域。我想用一些東西將主頁區域引用到所有其他區域,因此代碼更具可讀性和更短。 如何使用對象數組主頁進行編輯?所以代碼變得更短。像這樣_images.edit.areas = _images.homepage.areas?或者我應該在一個單獨的對象中定義主頁?如何獲取對象數組值到對象內的其他數組值?

var _images = { 
     homepage: { 
      path: 'homepage.jpg',  
      areas: [{  
       coords: '45,143,106,158', 
       text: 'edit',   
       goto: 'edit'  
       }, { 
       coords: '45,164,149,180', 
       text: 'config', 
       goto: 'config' 
       }, { 
       coords: '45,181,108,196', 
       text: 'setfree', 
       goto: 'setfree' 
       }] 
      }, 
     edit:{ 
      path: 'edit.jpg', 
      areas: [{  
       coords: '18,131,113,140', 
       text: 'homepage', 
       goto: 'homepage' 
       }, { 
       coords: '45,164,149,180', 
       text: 'config', 
       goto: 'config' 
       }, { 
       coords: '45,181,108,196', 
       text: 'setfree', 
       goto: 'setfree' 
       },{ //above everything is like in hompage, below are news areas 
      coords: '638,467,652,478', 
      text: 'contract',  
      goto: 'contract' 
      }] 
      }, 
     } 

回答

0

這取決於。隨着作業

_images.edit.areas = _images.homepage.areas; 

你從hompage參考編輯。在主頁部分或編輯部分的每個變更都可以在兩個對象上使用,因爲它們是鏈接在一起的。

如果你想要的話,那很好 - 但如果沒有,那麼我建議使用兩個獨立的對象。

+1

好的,謝謝。那麼,我會分開他們^^ – Emloy