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 trial

JavaScript Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Making the UI Work

David Curran
David Curran
7,682 Points

DOT syntax

I am a little confused with some of the coding concepts in this chapter of OOP. Why does Andrew do the following?

Playlist.prototype.play = function() { var currentSong = this.songs[this.nowPlayingIndex]; currentSong.play(); };

The this.nowPlayingIndex is the index of the array songs. Therefore variable currentSong will then hold the properties title, artist and duration of that index element. But why write currentSong.play(); and not playlist.play();

1 Answer

Steven Parker
Steven Parker
231,269 Points

The purpose of curentSong.play() is to begin playing a song.

There are two different methods in this project named play. The one being defined here is for a Playlist object, which will then call the play method on a Song object. And currentSong is a Song object that was chosen from the list of songs using nowPlayingIndex.

Calling playlist.play() here would be an improper reference to the global playlist, and would also cause an infinite recursion loop, since it is that method which is being defined here.