-2

I need a custom object for a staff member so I have this in staff.js:

    function Staff(name, shouldBeRemoved, officeName, dateHired) {
        this.name = name;
        this.shouldBeRemoved = shouldBeRemoved;
        this.officeName = officeName;
        this.dateHired = dateHired;
    }

I also have a custom object for an Office in office.js:

    function Office(name, shouldBeRemoved, staffMembers) {
        this.name = name;
        this.shouldBeRemoved = shouldBeRemoved;
        this.staffMembers = staffMembers;
    }

I want to use the staffMembers property to be an array of Staff objects inside an Office.

But when I try to push a new staff object into a staffMembers array in my Office object, nothing happens:

staff = new Staff(name, false, officeSelected, d.toDateString());
        for (i=0; i<officeArray.length; i++) {
            if (officeArray[i].name == officeSelected) {
                officeArray[i].staffMembers.push(staff);
            }
        }
  • What do you mean, "nothing happens"? – Pointy Jul 15 '17 at 18:06
  • The line `officeArray[i].staffMembers.push(staff);` doesn't run. and nothing runs after it. If I try to open an alert window after that, it won't open. – R Tierney Jul 15 '17 at 18:10
  • Do you have a js fiddle? What does your console say? Are you getting errors or is staffMembers null? – JonLuca Jul 15 '17 at 18:11
  • No I've never used js fiddle, I will try that. I think staffMembers is just null. Let me see if I can find out. (Sorry, I'm a little new to javascript) – R Tierney Jul 15 '17 at 18:13
  • Are getting an error reported? What does the construction of the `Office` instances look like; are you sure that `staffMembers` is being initialized to a new empty array? – Pointy Jul 15 '17 at 18:14
  • You can also use [stacksnippets](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/) or [plnkr](https://plnkr.co) to create a [minimal, complete verifiable example](https://stackoverflow.com/help/mcve). Where is `officeArray` defined? – guest271314 Jul 15 '17 at 18:15
  • I can say that there is no error reported. I'll need to learn how to interpret the console to see how everything is being initialized and changed when accessed. I don't know how to do that right now. – R Tierney Jul 15 '17 at 18:18
  • officeArray is defined at the top of my main.js file: `var officeArray = [];` – R Tierney Jul 15 '17 at 18:19
  • Ok I think I see an issue, I'm not instantiating the array to be of Office types. – R Tierney Jul 15 '17 at 18:21
  • Sorry Pointy, Yes I am getting an error reported in the console. It says "Cannot read property 'push' of undefined." So I am going to say that I am not properly defining my array Staff inside Office objects. This is happening when I say `officeArray[i].staffMembers.push(staff);` – R Tierney Jul 15 '17 at 18:46
  • actually I removed a window.alert wrapper in my code and the console is saying "Uncaught TypeError: officeArray[i].staffMembers.push is not a function at createStaffButtonClicked" – R Tierney Jul 15 '17 at 18:49
  • So I guess my question should be, In my Office class definition, how can I instantiate an array of Staff data types. Basically, my object oriented programming skills are lacking in javascript. – R Tierney Jul 15 '17 at 18:53

1 Answers1

0

My suggestion would be to start using Typescript, the reason being that the datatypes and classes have a clear picture.

 staff = new Staff(name, false, officeSelected, d.toDateString());
    for (i=0; i<officeArray.length; i++) {
        if (officeArray[i].name == officeSelected) {
            officeArray[i].staffMembers.push(staff);
        }
    }

here u should check officeArray[i].name === staff.officeSelected