2016-12-31 111 views
1

我有一個問題。製作另一個對象內的對象的副本。 JAVASCRIPT

我有一個簡單的物體,像這樣:

var simpleObject = { 

    name = "John", 
    favColor = "blue 
} 

和另一個對象,具有更多的特性和物體裏面:

我想提出simpleObject副本這樣的大物體內:

biggerObject.copyofSimpleObject = simpleObject; 

但是,每當我改變simpleObject的值,greaterObject.copyofSimpleObject的值也會發生變化。

因此,它似乎是對原始對象的引用,這是一件很酷的事情,但在這種情況下,我需要它作爲副本,以便當原始simpleObject更改greaterObject.copyofSimpleObject值時保持不變。我知道這可能很簡單,但我不知道該怎麼做,因爲我是一個noob。請幫助:)謝謝!這是我使用的所有JavaScript。

回答

0

你可以簡單地做:

var bigObject = {copyObject:simpleObject} 

,並定義simpleObject這樣的:

var simpleObject = { 
    name : "John", 
    favColor :"blue" 
} 

然後你就可以使用訪問:

bigObject.simpleObject 
相關問題