What I am trying to achieve is:
- Declare an Array that will house ALL the projects.
- A constructor makes an object which will be a new project. The constructor then immediately pushes it to the
allProjectarray. - I then want to display certain variables from the object like
._hoursafter it is in the array already.
My code entails:
//Declare Array that will Host Projects
const allProjects = [];
//Create Parent Class that creates objects (Project)
class Project {
constructor(projTitle, projDescription, projHours) {
//Declare variables
this._name = projTitle;
this._description = projDescription;
this._hours = projHours;
//Send newly constructed object directly to next space in array.
allProjects.push(constructor(projTitle, projDescription, projHours));
}
}
//Create new object.
const DataEntry = new Project('Data Entry', 'Enter Data into Amazon', '4 Hours');
//Display the name variable housed in the object that was pushed into allProjects array.
console.log(allProjects[0].DataEntry._name);
The output looks like this:
/Users/ervin/WebstormProjects/untitled/Project Management.js:30
console.log(allProjects[0].DataEntry._name);
^
TypeError: Cannot read property '_name' of undefined
at Object.<anonymous> (/Users/ervin/WebstormProjects/untitled/Project
Management.js:30:38)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
Process finished with exit code 1
Any help is taken into consideration and appreciated. Let me know if I need to clarify anything.