I am working on a JavaScript objects exercise for a course I’m following, but when I run the code I don’t get the expected result. I’ve cut the code down to the essentials for clarity.
const myObject = {
_array: [
{item: 'a', item2: 'b', item3: 'c'},
{item: 'd', item2: 'e', item3: 'f'},
{item: 'g', item2: 'h', item3: 'i'}
],
get items () {
return this._array;
},
addThing (newItem1, newItem2, newItem3) {
let thing = {
item1: newItem1,
item2: newItem2,
item3: newItem3
};
/* `this.players.push(player);` is adding a new player object to the `_players` array of the `team` object. It uses the `push()` method to add the `player` object to the array. */
this._array.push(thing);
}
}
console.log('hello before');
myObject.addThing('j', 'k', 'l');
console.log(myObject._array);
console.log('hello after');
Running this from a terminal window gives me the expected result where the array is dumped …
… but running it with F5 or Run and Debug gives me this
I’d like to understand why there is no output of _array when run this way.