Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialZak Erving
22,832 PointsWhy 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?
2 Answers
Kelly von Borstel
28,880 PointsInside 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
28,880 PointsThe 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
Zak Erving
22,832 PointsOh! I see it now! That makes all the sense in the world :) Thank you so much!
Kelly von Borstel
28,880 PointsHi, 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.
Zak Erving
22,832 PointsThat'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?
Zak Erving
22,832 PointsZak Erving
22,832 PointsAlso, when do I know that some functions require parameters and others don't?