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.
ggorlen
59.5k9 gold badges119 silver badges173 bronze badges
asked Aug 17, 2018 at 22:54
Ervin Enriquez
571 gold badge1 silver badge7 bronze badges
-
You should really look into how modules work if you're going to be using Node.js (which I'm assuming from that stack trace you are). Generally, one doesn't use global variables when writing module-based code.Heretic Monkey– Heretic Monkey2018年08月17日 23:12:24 +00:00Commented Aug 17, 2018 at 23:12
-
@HereticMonkey, Thanks for that. I just looked it up, it looks interesting.Ervin Enriquez– Ervin Enriquez2018年08月18日 00:08:05 +00:00Commented Aug 18, 2018 at 0:08
2 Answers 2
Update this line in your constructor
allProjects.push(this);
I tested it, it is working as you intended.
answered Aug 17, 2018 at 23:16
manntsheth
4506 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can access your ._name like this: allProjects[0]._name
But you're actually pushing the wrong way. You will receive the parameters after you call new Project(...), so push like this:
//Declare Array that will Host Projects
const allProjects = [];
class Project {
constructor(projTitle, projDescription, projHours) {
this._name = projTitle;
this._description = projDescription;
this._hours = projHours;
}
}
//Create new object.
const DataEntry = new Project('Data Entry', 'Enter Data into Amazon', '4 Hours');
allProjects.push(DataEntry)
//Display the name variable housed in the object that was pushed into allProjects array.
console.log(allProjects[0]._name)
Not tested at all, but should work!
Comments
lang-js