Javascript 如何将一个类实例推送到另一个类的私有数组中?

Javascript 如何将一个类实例推送到另一个类的私有数组中?,javascript,arrays,class,inheritance,Javascript,Arrays,Class,Inheritance,我的课程如下所示: class RoomUsageList { constructor() { // private atributes this._roomList = []; } set roomList(newIns) { this._roomList.push(newIns); } } function saveTap() { // create a new class instance when the SAVE button // is pushed.

我的课程如下所示:

class RoomUsageList {

constructor()
{
    // private atributes
    this._roomList = [];

}

set roomList(newIns)
{
    this._roomList.push(newIns);
}
}
function saveTap()
{
// create a new class instance when the SAVE button
// is pushed.
let newAddress = document.getElementById('address').value;
let newRoomNo = document.getElementById('roomNumber').value;
let newLightSwitch = document.getElementById('lights').checked;
let newHeatCoolSwitch = document.getElementById('heatingCooling').checked;
let newSeatsUsed = document.getElementById('seatsUsed').value;
let newSeatsTotal = document.getElementById('seatsTotal').value;


let roomUsageIns = new RoomUsage();
roomUsageIns.address = newAddress;
roomUsageIns.roomNumber = newRoomNo;
roomUsageIns.lightsOn = newLightSwitch;
roomUsageIns.heatingCoolingOn = newHeatCoolSwitch;
roomUsageIns.seatsTotal = newSeatsTotal;
roomUsageIns.seatsUsed = newSeatsUsed;

// add the above-created class instance into the RoomUsageList class

let roomUsageList = new RoomUsageList(roomUsageIns);
roomUsageList.roomList = roomUsageIns;

console.log(roomUsageList);

}
以及一个函数文件(在另一个javascript文件中),该文件将另一个类(RoomUsage)的instace传递给RoomUsageList类(如上)的setter,如下所示:

class RoomUsageList {

constructor()
{
    // private atributes
    this._roomList = [];

}

set roomList(newIns)
{
    this._roomList.push(newIns);
}
}
function saveTap()
{
// create a new class instance when the SAVE button
// is pushed.
let newAddress = document.getElementById('address').value;
let newRoomNo = document.getElementById('roomNumber').value;
let newLightSwitch = document.getElementById('lights').checked;
let newHeatCoolSwitch = document.getElementById('heatingCooling').checked;
let newSeatsUsed = document.getElementById('seatsUsed').value;
let newSeatsTotal = document.getElementById('seatsTotal').value;


let roomUsageIns = new RoomUsage();
roomUsageIns.address = newAddress;
roomUsageIns.roomNumber = newRoomNo;
roomUsageIns.lightsOn = newLightSwitch;
roomUsageIns.heatingCoolingOn = newHeatCoolSwitch;
roomUsageIns.seatsTotal = newSeatsTotal;
roomUsageIns.seatsUsed = newSeatsUsed;

// add the above-created class instance into the RoomUsageList class

let roomUsageList = new RoomUsageList(roomUsageIns);
roomUsageList.roomList = roomUsageIns;

console.log(roomUsageList);

}
输出仅包含一个实例,不推送新的RoomUsage实例。为什么会这样? 我是编程新手,任何帮助都将是惊人的

输出:


问题是,每次调用
saveTap
函数时,您都在创建一个新的RoomUsageList实例。这是将
\u roomList
重置为
[]

如果在函数外部调用
roomUsageList=new-roomUsageList()
,则setter应按预期工作,并将新的
RoomUsage
实例附加到private
\u-roomList
属性

编辑:打字错误 EDIT2:在代码段中删除了额外的控制台日志

课堂使用{
构造函数(){
//
}  
}
教室传道者{
构造函数(){
//私人属性
这个。_roomList=[];
}
设置房间列表(newIns){
const roomList=[…这个。_roomList,纽因斯];
这个。_roomList=roomList
}
}
//创建全局状态
const roomUsageList=新的roomUsageList();
函数onFormSubmit(事件){
event.preventDefault();
//单击“保存”按钮时创建新的类实例
//被推。
让newAddress=document.querySelector('.address').value;
让newRoomNo=document.querySelector('.roomNumber').value;
让newLightSwitch=document.querySelector('.lights')。选中;
让newHeatCoolSwitch=document.querySelector('.heatingCooling')。选中;
让newSeatsUsed=document.querySelector('.seatsUsed').value;
让newSeatsTotal=document.querySelector('.seatsTotal').value;
let roomUsageIns=新的RoomUsage();
roomUsageIns.address=新地址;
roomUsageIns.roomNumber=newRoomNo;
roomUsageIns.lightsOn=newLightSwitch;
roomUsageIns.heatingCoolingOn=新建加热冷却开关;
roomUsageIns.seatsTotal=newSeatsTotal;
roomUsageIns.seatsUsed=使用的新闻;
//将上面创建的类实例添加到RoomUsageList类中
roomUsageList.roomList=roomUsageIns;
控制台日志(roomUsageList);
}

地址
房间号
灯
加热/冷却
使用的座位
座位总数
拯救

感谢您的帮助,通过稍微调整代码,程序运行得非常完美。这就是我所做的:

//Room Usage List Class
//---------------------
//
class RoomUsageList {

constructor()
{
    // private atributes
    this._roomList = [];

}

set roomList(newIns)
{
    // pushes new array into parent class instance
    this._roomList.push(newIns);
}  
}

//making the parent class instance global
const roomUsageList = new RoomUsageList();

function saveTap()
{
// create a new class instance when the SAVE button
// is pushed.
let newAddress = document.getElementById('address').value;
let newRoomNo = document.getElementById('roomNumber').value;
let newLightSwitch = document.getElementById('lights').checked;
let newHeatCoolSwitch = document.getElementById('heatingCooling').checked;
let newSeatsUsed = document.getElementById('seatsUsed').value;
let newSeatsTotal = document.getElementById('seatsTotal').value;

let roomUsageIns = new RoomUsage();
roomUsageIns.address = newAddress;
roomUsageIns.roomNumber = newRoomNo;
roomUsageIns.lightsOn = newLightSwitch;
roomUsageIns.heatingCoolingOn = newHeatCoolSwitch;
roomUsageIns.seatsTotal = newSeatsTotal;
roomUsageIns.seatsUsed = newSeatsUsed;

// add the above created class instance into the RoomUsageList class
roomUsageList.roomList = roomUsageIns;
console.log(roomUsageList);

}

您应该仅使用
setter
来设置整个列表,而不是附加项-为此,您可以创建单独的方法,例如用于附加单个项的
addItem
,或将多个项插入数组的
addItems

class RoomUsageList {

  constructor() {
    this._roomList = [];
  }

  set roomList(roomList) {
    this._roomList = roomList;
  }

  addItem(item) {
    this._roomList.push(item);
  }

  addItems(items) {
    this._roomList.concat(items);
  }
}

很好,您找到了解决方案—每次调用函数都会创建新实例:)

它正在推送新实例。您只是没有在构造函数中使用参数。代码正在按预期工作。我想你的问题是每次调用
saveTap
时都要重新定义
roomUsageList
。实际上你的
RoomUsage
类应该有一个
.roomList
属性。根本不应该有
RoomUsageList
课程。保持简单。如果要
(附加)推送到列表中,请不要使用setter。或者,如果您不想附加到setter中的列表,但要覆盖列表,请完全删除setter。感谢您注意到这一点。但是在尝试代码之后,通过创建新数组、在数组中、在数组中等等来推送新数组。我刚刚修改了你的代码,现在它工作得很好,再次感谢你的帮助!(我添加了我刚才所做的作为对这个问题的回答)。嗯,当我运行我的代码片段时,情况并非如此。我的代码使用spread操作符来确保一个平面列表:
constroomlist=[…this.\u roomList,newIns]你也这么做了吗?无论如何,我认为DarekGala的答案是更好的API设计。应使用setter设置值,而不是部分修改它。因此,最好使用公共方法从私有列表中添加/删除元素。