Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Playlist Project

Why does the add method want to take the parameter of 'song'?

At around 3:30 in this video, Andrew says that the add method of Playlist.prototype.add wants to take the parameter 'song', but I have no idea why…'Song' is a constructor function, but that doesn't allow the playlist app to function normally.

Is 'song' just an arbitrary name passed as a parameter because it's the singular state of the 'songs' array?

Also, when do I know that some functions require parameters and others don't?

2 Answers

Kelly von Borstel
Kelly von Borstel
28,879 Points

Inside the Playlist function, on line 2, 'this.songs' is assigned an empty array, []. And then on line 7, a song is 'pushed' to that array. The names 'song' and 'songs' could be anything. For example, you could use 'songName' (instead of song) and 'listOfSongs' (instead of songs). Having the array name be the plural of the individual array element name makes sense in this case, but it's not required.

Kelly von Borstel
Kelly von Borstel
28,879 Points

The song isn't automatically added — it's added using the push method, which is a native array method available to any array. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Oh! I see it now! That makes all the sense in the world :) Thank you so much!

Kelly von Borstel
Kelly von Borstel
28,879 Points

Hi, Zak. You're correct, 'song' is just an arbitrary name. You can think of it sort of like a variable name. When the function is called and an argument (some specific song) is passed in, the parameter 'song' takes on that value, which is then pushed to the 'songs' array.

Whether a function needs parameters or not can be confusing. But if you think about what a function is doing, it will help you know what input (parameters) it needs. In this case, the add method is adding a song to an array of songs. Without the song parameter (so that a song can be passed in as an argument), how will the function know what song to add to the array? If that doesn't make sense, let me know, and I'll try to explain it better.

That's very helpful, Kelly. Thank you :)

But how does "song" get automatically added to the "songs" array? Is there some sort of pluralization mechanism in JavaScript that I don't know about?