Finishing OOP JavaScript at Treehouse | Tiffany R. White Blog

Finishing OOP JavaScript at Treehouse

Reading time: 4 mins

OOP JavaScript

We learned a bit about constructors and prototypal inheritance in OOP JavaScript in this final section. We also went over prototype chaining which I found interesting.

Inheritance of Properties Through the Prototype Chain

Every JavaScript object has a Prototype object and inherit from Object.prototype. You cannot inherit from classes in JavaScript as there is no “class” per se. You only inherit from other objects.

In order to spread functionality throughout different functions and scopes, we can chain different objects together using the inherent nature of Object.prototype. We can do this a couple of ways but in the Treehouse example, we use Object.create() to chain together two objects.

Take for example this code:


function Media() {

  this.title: = title:;

  this.duration = duration;

  this.isPlaying = false;

}

Media.prototype.play = function() {

  this.isPlaying = true;

};

Media.prototype.stop = function() {

  this.isPlaying = false;

};

This is an example of a constructor with some functionality. What if I have another constructor that I want to inherit properties from this constructor? You could do this:


function Movie() {

  Media.call(this, title:, duration);

  this.year = year;

}

Movie.prototype = Object.create(Media.prototype);

Movie.prototype.stop = function() {

  this.isPlaying = false;

};

The Movie() constructor is inheriting properties from the Media() constructor.

The call() Method

You can also use call() to chain constructors for an object. In the example above, we’ve used:


function Movie() {

  Media.call(this, title:, duration);

  this.year = year;

}

The call() method always takes two or more arguments, with this always being the first, which refers to the current object, or the calling object.

The Final Project for OOP JavaScript at Treehouse

I was lost. I honestly don’t know how I was supposed to come up with the answers provided. I didn’t know where to start, which was honestly my fault. I didn’t take very good notes while going through the course at the end. But I wasn’t the only one stumped by this challenge. I don’t feel I was prepared enough for it, regardless of how many notes I took. You can find the gist of the solution down below.

https://gist.github.com/twhite96/42aaf04d874630e4c733236392dc23f7

OOP JavaScript

We learned a bit about constructors and prototypal inheritance in OOP JavaScript in this final section. We also went over prototype chaining which I found interesting.

Inheritance of Properties Through the Prototype Chain

Every JavaScript object has a Prototype object and inherit from Object.prototype. You cannot inherit from classes in JavaScript as there is no “class” per se. You only inherit from other objects.

In order to spread functionality throughout different functions and scopes, we can chain different objects together using the inherent nature of Object.prototype. We can do this a couple of ways but in the Treehouse example, we use Object.create() to chain together two objects.

Take for example this code:


function Media() {

  this.title: = title:;

  this.duration = duration;

  this.isPlaying = false;

}

Media.prototype.play = function() {

  this.isPlaying = true;

};

Media.prototype.stop = function() {

  this.isPlaying = false;

};

This is an example of a constructor with some functionality. What if I have another constructor that I want to inherit properties from this constructor? You could do this:


function Movie() {

  Media.call(this, title:, duration);

  this.year = year;

}

Movie.prototype = Object.create(Media.prototype);

Movie.prototype.stop = function() {

  this.isPlaying = false;

};

The Movie() constructor is inheriting properties from the Media() constructor.

The call() Method

You can also use call() to chain constructors for an object. In the example above, we’ve used:


function Movie() {

  Media.call(this, title:, duration);

  this.year = year;

}

The call() method always takes two or more arguments, with this always being the first, which refers to the current object, or the calling object.

The Final Project for OOP JavaScript at Treehouse

I was lost. I honestly don’t know how I was supposed to come up with the answers provided. I didn’t know where to start, which was honestly my fault. I didn’t take very good notes while going through the course at the end. But I wasn’t the only one stumped by this challenge. I don’t feel I was prepared enough for it, regardless of how many notes I took. You can find the gist of the solution down below.

https://gist.github.com/twhite96/42aaf04d874630e4c733236392dc23f7

See what I mean? We didn’t learn half this stuff. I love Treehouse and this was a hard course for Andrew to teach. But I felt it sorely lacking in practice.


Share

No Ads. No Sponsored Content. Ever.

I've decided that I no longer want to have sponsored posts on this blog. I also, though as tempting as it may sound, don't want ads; even if I got big enough for Carbon ads I don't want them. I want to post free, engaging content for beginning/junior devs. This means that I won't have Setapp posts or affiliates on this blog, though I will keep them on the Uses page. If you enjoy the content, buy me a coffee. Just click the button below.


Coffee Buy me a coffeeBuy me a coffee


Webmentions 0

No webmentions were found.

Newsletter