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) Prototypal Inheritance Building the Movie Object

different between Object.create and new?

different between Object.create and new

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi there.

From what I understand, there really isn't any difference, as both will create a new Object. I found two MDN documentations that may help here.

MDN Object-Oriented Programming. For this one, scroll just under half way down the document to "Custom Objects.

MDN Object.create().

I hope this helps. Keep Coding! :)

Joshua Erskine
Joshua Erskine
7,706 Points
  • Object.create() passes/overwrites an object with another object (In this case Media.prototype onto Song.prototype).
  • new generates an object from a function.

This app can be built without either of these.
By changing:

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

to:

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

Same goes for stop and next. Now Media.prototype.play(), stop(), next() are redundant and you don't need to share them with Movie.prototype and Song.prototype.

I changed the order of

Song.prototype = Object.create(Media.prototype)

and

Song.prototype.toHTML = function() {},

and it does not work. So, I suppose Object.create() really create some whatever object that has a prototype of Media.prototype. Then, the code assigns this whatever object to Song.prototype. Then, this whatever object that is now the prototype of song object and has its prototype of Media.prototype holds a method .toHTML().

That's why if Song.prototype.toHTML=function() goes before Song.prototype=Object.create(Media.prototype), then it doesn't work because Song.prototype.toHTML was overwritten.