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 trialMohammed Safiulla D
17,044 PointsRegarding the order of linking js files in index.html. Why is it necessary to link media.js before song.js?
I know that song.js uses function Media from media.js, but if that's the case then why is playlist.js before song.js, as playlist.js also uses song object. What I had previously thought was that since app.js is the file which actually creates the objects whose definitions are in other script files, it didn't matter in what order the files were linked as long as all are called before app.js ( and had tried that out before changing the order of calling playlist.js and song.js ), but now it doesn't seem to work unless media.js is called before song.js. Can someone explain why its ok when we interchange the order of playlist.js and song.js, but not media.js and song.js?
1 Answer
Özgür Ozan Çakmak
11,451 PointsI haven't reached to the object-oriented javascript
course yet but what I know is you need to link the javascript files in order they are used. What I mean is if you are using jquery libraries in a file called app.js
for example, you need to include jQuery first, then your app.js
because it wouldn't function without the jQuery definition.
If those objects you mention don't depend on each other, common sense says you can link them in whatever order you wish. If they do, the depending file must be linked after the depended file.
You can think them like inverted blocks, rising on top of each other. First <script src...
goes to the base, and second rises on top of that and so on and so forth.
Mohammed Safiulla D
17,044 PointsMohammed Safiulla D
17,044 PointsYes, I get that, but here in this course playlist.js uses an object of song.js, but its called before song.js (in index.html) and it seems to work fine.
Özgür Ozan Çakmak
11,451 PointsÖzgür Ozan Çakmak
11,451 PointsHm.. weird. Can you copypaste the code in the html and the javascripts of playlist.js and song.js, as well as the app.js if there is any.
Mohammed Safiulla D
17,044 PointsMohammed Safiulla D
17,044 PointsHere is the link to my workspace https://w.trhou.se/wksxk8cd3f
Özgür Ozan Çakmak
11,451 PointsÖzgür Ozan Çakmak
11,451 PointsAh okay now it is clearer!
I assume the
this.songs=[]
statement and the operations done to it is confusing you.The
songs
property and all the operations done in theplaylist.js
has nothing to do with thesong.js
. It only creates a property in the Playlist object, which is an array.You instantiate the song object via
new Song("Hey Jude!");
or (which is a new usage for me I have to admit) Song.call() (like theMedia.call(this,title,duration);
statement onplaylist.js
) which isn't used yet.Please note the capital S here which is derived from the statement
function Song(title,artist,duration) {
onsong.js
. If you named it asSongTree
in here, you'd dovar song = new SongTree("Hey Jude");
orSongTree.call(something,something)
Hope it is clear.
Mohammed Safiulla D
17,044 PointsMohammed Safiulla D
17,044 PointsThanks! So basically to sum up what you said, since we are not instantiating a song object in playlist.js (this.songs = []; is just declaring of an empty array not instantiating), so it's ok call playlist.js before song.js or use prototype methods of song.js (note that in play() and stop() methods currentSong.play() and currentSong.stop() are calling prototype methods of song.js).
Özgür Ozan Çakmak
11,451 PointsÖzgür Ozan Çakmak
11,451 PointsExactly! Glad I can be of assistance )))