私はオブジェクトの配列を持っている(このオブジェクトはまた、それ自身の配列が含まれています)私はなぜわからないんだけど、私はオブジェクトの配列内のオブジェクトの1つのインスタンスのメンバーアレイ上にいくつかの値を押すと、それはまたようです他のすべてのオブジェクト配列上の他のメンバ配列にプッシュします。私は以下のコードを提供しています:オブジェクトのJavascript配列、同じメンバ配列を共有するオブジェクト?
var ImageGroup = {
GroupName:"",
haz:[]
};
var ImageHandler = {
ImageGroupsArray:[],
image_process: function() {
//Calling the function here...
//Pushing the objects
this.ImageGroupsArray.push(Object.create(ImageGroup));
this.ImageGroupsArray.push(Object.create(ImageGroup));
//Assigning some values
this.ImageGroupsArray[0].haz.push("Dog");
this.ImageGroupsArray[1].haz.push("Cat");
//Should output array with only 'Dog' in it
console.log(this.ImageGroupsArray[0].haz);
//Should output array with only 'Cat' in it
console.log(this.ImageGroupsArray[1].haz);
//Instead, both of these output ["Dog","Cat"]
//this.ImageGroupsArray[1].haz and this.ImageGroupsArray[0].haz point to same 'haz' array??
}
}
同じようにGroupNameを設定しようとすると、これは起こりません。私は間違って何をしていますか?事前に助けてくれてありがとう!
ここで 'ImageGroupsArray'は定義されていますか? –
完全な作業コードを教えてください – Guillaume
'ImageGroupArray'で' ImageGroup'オブジェクトをどのように押していますか? –