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

are play() and stop() functions built in javascript functions?

This might be a silly question, but I don't understand how the play() and stop() methods work. I don't see the play or stop functions created anywhere in the code so i'm now sure how the browser would know what to do when they are called. I'm guessing there is something pretty obvious i'm missing?

In case this doesn't post to the correct video I am referencing Object Oriented Programming course taught by Andrew Chalkey when he is created a media player.

1 Answer

Neil McPartlin
Neil McPartlin
14,662 Points

Hi Britin. As the saying goes, there are no silly questions, just questions that others were afraid to ask. I did this course a few months back and realised that the play() and stop() functions were working, but I hadn't remembered specifically creating them and here is why.

In this video... https://teamtreehouse.com/library/objectoriented-javascript/constructor-functions-and-prototypes/creating-multiple-instances-with-constructors ...Andrew introduces us to the Constructor Function which can be spotted by the convention that the first letter of the function name is capitalised. Until now we had been creating 'object literals' (once off objects) and now we need a way of creating multiple objects sharing a similar format. Enter the Constructor Function.

Later, in this video... https://teamtreehouse.com/library/objectoriented-javascript/constructor-functions-and-prototypes/methods-with-prototypes ...Andrew introduces us to Prototypes. He explains that if we are not careful when creating Constructor Functions, every time the functions gets run, we generate multiple anonymous functions that start to fill up our RAM. Such an anonymous function could be moved out from the Constructor Function, but then we could confuse ourselves with named functions that are only meant to be used by a specific Constructor Functions. Enter the Prototype property which enables us to organise our code.

Now if you look at this video... https://teamtreehouse.com/library/objectoriented-javascript/constructor-functions-and-prototypes/playlist-project ... at 1:57 you get a brief and first glimpse of playlist.js which has come pre-filled with what Andrew refers to as 'scaffolding for our object' see below...

function Playlist() {

  }

  Playlist.prototype.add = function() {

  };

  Playlist.prototype.play = function() {

  };

  Playlist.prototype.stop = function() {

  };

  Playlist.prototype.next = function() {

  };

  Playlist.prototype.renderInElement = function(list) {

  };

The video goes on to explain the contents of each of the 5 'functions' including play() and stop().

Thank you!