这对我来说非常令人沮丧,所以我希望有人能提供帮助。下面是一个小例子来说明我的问题。
var group = {
names: []
}
var groupList = {
group: [],
activity:[]
}
$(document).ready(function(){
$("#play").click(function(){
var soccer = group;
var person = "Jack";
soccer.names.push(person);
var person = "Amber";
soccer.names.push(person)
groupList.group.push(soccer); //group[0]
// how do I make a constructor for a new group?
var soccer1 = group;
var person = "Jill";
soccer1.names.push(person)
groupList.group.push(soccer1); //group[1]
// expect group 0 to be [Jack,Amber] (but pushing Jill on group1 updates group0)
$("#beta").append("Soccer Group: "+groupList.group[0].names +"<br/>");
// expect group 1 to be either [Jack,Amber,Jill] or [Jill]
$("#beta").append("Soccer Group: "+groupList.group[1].names +"<br/>");
});
});
我需要在不更改原始组的情况下创建一个新的组实例。