0

What I am trying to achieve is:

  1. Declare an Array that will house ALL the projects.
  2. A constructor makes an object which will be a new project. The constructor then immediately pushes it to the allProject array.
  3. I then want to display certain variables from the object like ._hours after 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.

2
  • 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. Commented Aug 17, 2018 at 23:12
  • @HereticMonkey, Thanks for that. I just looked it up, it looks interesting. Commented Aug 18, 2018 at 0:08

2 Answers 2

1

Update this line in your constructor

allProjects.push(this);

I tested it, it is working as you intended.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.