2017-04-12 62 views
1

在JS中我是全新的。請有人幫助我嗎?我需要準備一些對象屬性,這取決於其他對象的屬性如何從一個屬性訪問其他對象atributins

我該如何存檔?

我試過了解決方案:

var Photo = { 
     current: { 
     coordinates: "2208,1922", 
     name: "current" 
     }, 
     start: { 
     coordinates: '2408,1822', 
     name: 'start', 
     newCoordinates: Photo.current.coordinates 
     } 
    }; 

我得到一個錯誤:

module initialization error: TypeError 

回答

2

你只需要等待初始化的對象,那麼你可以抓住該屬性。看到這個代碼:

var Photo = { 
 
     current: { 
 
     coordinates: "2208,1922", 
 
     name: "current" 
 
     }, 
 
     start: { 
 
     coordinates: '2408,1822', 
 
     name: 'start' 
 
     } 
 
    }; 
 
    
 
    Photo.start.newCoordinates = Photo.current.coordinates; // 2208,1922

1

var current = { 
 
     coordinates: "2208,1922", 
 
     name: "current" 
 
     } 
 

 
var Photo = { 
 
     current: current, 
 
     start: { 
 
     coordinates: '2408,1822', 
 
     name: 'start', 
 
     newCoordinates: current.coordinates 
 
     } 
 
    }; 
 
    
 
    console.log(Photo);