2016-09-23 62 views
1

我想知道在下面的代碼中處理複製一個對象到另一個對象上發生了什麼。在某些情況下,他們的行爲就像是改變另一個的相同對象。我發現很多關於JavaScript對象如何通過引用被複制的帖子,所以它們實際上是相同的對象。例如,從http://www.w3schools.com/js/js_object_definition.asp一個案例中的對象相同,另一個案例中的對象不同?

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"} 
var x = person; // This will not create a copy of person. 
x.age = 10; // This will change both x.age and person.age 

The object x is not a copy of person. It is person. Both x and person 
is the same object. Any changes to x will also change person, because 
x and person are the same object. 

不過,我也發現那裏的對象似乎是單獨的對象的情況。在一種情況下,它如何表現爲相同的對象,但在另一種情況下,卻有不同的對象?我將不勝感激情況任何光:

例子:http://codepen.io/gratiafide/pen/yagQGr?editors=1010#0

HTML:

<div id="app"> 
    <my-component> 
    </my-component> 
</div> 

JS:

var MyComponent = Vue.extend({ 
    template: '<div v-on:click="test()" class="red">1. Click here to copy the name object to message and change the value of name to see if the value of message gets changed also. (It does not).</div> message: {{ message | json}} <br> name: {{ name | json}}<div v-on:click="test2()" class="red">2. Now click here to see if updating the name object also changes the message object. It does! Why here and not in #1?</div><div v-on:click="test3()" class="red">3. Click here to see yet another way of updating the name object also changes the message object. Why here and not in #1?</div>', 

    data: function() { 
    return { 
    message: {}, 
    name: {} 
    } 
    }, 

    ready: function(){ 
    this.message = {}; 
    }, 
    methods: { 
    test: function(){ 
     this.message = {'t1':"m1"}; 
     this.name = this.message; 
     this.name = {'t2':"m2"}; 
    }, 
    test2: function(){ 
     this.message = {'t1':"m1"}; 
     this.name = this.message; 
     for (var key in this.name) { 
       this.name[key] = ''; 
      } 
    }, 
    test3: function(){ 
     this.message = {'t1':"m1"}; 
     this.name = this.message; 
     Vue.set(this.name, 't1', 'm2'); 
    } 
    } 
}); 

Vue.component('my-component', MyComponent); 

new Vue({ 
    el: '#app' 
}); 

CSS:

@import url(https://fonts.googleapis.com/css?family=Open+Sans); 

.red{ 
    color:red; 
} 

body { 
    font-family: 'Open Sans', sans-serif; 
    background: rgba(0,0,0,.5); 
    margin: 0; 
} 

#app { 
    width: 500px; 
    margin: 0 auto; 
    padding: 10px 20px; 
    background: rgba(255,255,255,.9); 
} 
+0

在JavaScript中,*賦值*(或方法參數)不*創建所述對象的副本/克隆/副本。此規則也適用於*其他對象內的屬性*。 – user2864740

+0

[JavaScript是傳遞引​​用還是傳值語言?]的可能的重複(http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass按值語言) – vlaz

+2

如果你做'a = {}; b = a;'和操作'b',你改變_same_對象。如果你做'a = {}; b = a; b = {}'now'b'指向一個_different_對象,所以操縱一個不會改變另一個。這幾乎是一切,真的。 – vlaz

回答

2

基本上,你問荷蘭國際集團這三者之間的區別:

this.message = {'t1':"m1"}; 
this.name = this.message; 

#1: 
this.name = {'t2':"m2"}; 

#2 
for (var key in this.name) { 
    this.name[key] = ''; 
} 

#3 
Vue.set(this.name, 't1', 'm2'); 

在第一種情況下,因爲你分配一個全新的對象this.name你會不會改變this.message。這個新對象({'t2':"m2"})與this.message完全無關。

做的也許是你所想/想法是:

this.name.t2 = "m2"; 

哪個做同樣的#2,#3,影響this.message,因爲this.name仍然指的是同一個對象。

要設定新的屬性到現有的對象,從另外一個對象,你可以在支持此功能的瀏覽器使用Object.assign

Object.assign(this.name, {'t2':"m2"}); 
1

有2種類型的變量,值和引用。所有的原語(即字符串,數字和布爾值)都是按值存儲的,其他所有的都是由引用存儲的,並且可以具有屬性。

var a,b; 
 
a={c:1}; 
 
b=a; 
 
console.log (b===a); // true; 
 

 
b={c:1}; // note that b is now being assigned a new reference. 
 
console.log (b===a); // false; 
 

 
a=b; // now a is being assigned the reference of b. 
 
console.log (b===a); // true; 
 

 
a.c=2; //note that it is the property of a (c) got changed, not a itself. 
 
console.log (b===a); // true; 
 

 
a.a = a; //now a has a property (a) that stores the reference of a. 
 
console.log(a === a.a); 
 
console.log(a.a === a.a.a); 
 
console.log(a.a === a.a.a.a.a.a.a.a.a); 
 
a.a.a.a.a.a.a.a.a.a.a.c = 10; 
 
console.log(a.c)//10;
有不同的方法來聲明變量,但這不是問題的範圍。

相關問題