Https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/team-stats

I was doing the exercise with advanced object manipulation in JavaScript (the one in the link where you have to make a sports team program) and I wrote a method to add a player to my team. This method adds a player with a first name, last name and an age to the array _players: .

I’m not sure why but in my first attempt I simply wrote it like this:

addPlayer(firstName, lastName, age){
this.players.push({firstName, lastName, age});
},

And it works. But I don’t understand why. The proper syntax (and the suggested one in the tip) should be:

const team = {

addPlayer(firstName, lastName, age) {
let player = {
firstName: firstName,
lastName: lastName,
age: age
};

this.players.push(player);

},

};

But when I use the first method I get the correct result which adds a player object to the nested array - like so:

team.addPlayer(“Steph”, “Curry”, 28);
// adds a player like so: _players: [ { firstName: ‘Steph’, lastName: ‘Curry’, age: 28 } ]

This method pushes an object that has {“Steph”, “Curry”, 28} to the nested _players array. But how does it match the keys to the values correctly? I mean how does it determine that “Steph” is the value of the key firstName? Is this a short-hand notation that wasn’t explained that I just randomly discovered or am I misunderstanding something?

there is a shorthand indeed:

Object Property Value Shorthand in JavaScript with ES6

which you can use when your key and value have the same name

Are method/function parameters and arguments clear? or did the short-hand confuse you?

I get how the suggested method works with all its’ parameters and arguments. But I don’t get why my own works - this:

addPlayer(firstName, lastName, age){
    this.players.push({firstName, lastName, age});
} // results in _players: [ { firstName: ‘Steph’, lastName: ‘Curry’, age: 28 } ]

It seems to invent keys named “firstName”, “lastName” and “age” - I’m not telling it to do that? Even if it’s correct. They’re just strings and a number when they’re entered into the method ie. teams.addPlayer(“Steph”, “Curry”, 28);

But they end up as key-value pairs. So if I understand it correctly: When the method is called, the program takes the name of the parameter in the method and turns that into the key and takes the value of the parameter and turns that into the matching value?

Did you read the link I posted about Property value shorthand?

this:

this.players.push({firstName, lastName, age});

is a short-hand for:

this.players.push({
firstName: firstName,
lastName: lastName,
age: age
});

which only works if properties and value are the same.

Ah, I did read it but I misunderstood it. Thanks, I understand it now.