如何引用一個對象而不是複製它?如下面的代碼所示,當調用o = 1
時,顯然o2
保持不變。如果我想讓o2
更改o
更改時如何操作?如何引用對象而不是複製它?
var o = {
a: {
b:2
}
};
// 2 objects are created. One is referenced by the other as one of its property.
// The other is referenced by virtue of being assigned to the 'o' variable.
// Obviously, none can be garbage-collected
var o2 = o; // the 'o2' variable is the second thing that
// has a reference to the object
o = 1; // now, the object that was originally in 'o' has a unique reference
// embodied by the 'o2' variable