Digging Into OOP JavaScript

Tiffany White,

On Treehouse, I am going through their Full-stack JavaScript track and we came up on OOP JavaScript.

Learning about Objects, state, and the origin and usage of this is intriguing as I have learned a bit about OOP through Java during my time at Pitt.

JavaScript doesn’t have regular Classes and instead uses Prototypes. Classical inheritance is seen as a big problem in JavaScript, which when you consider JavaScript doesn’t have real Classes, it makes sense. Brandon Eich, the creator of JavaScript admitted he wanted JavaScript to look like the little brother of Java, which leaves us with syntax that looks like Java when we use things like Constructors. For instance, in Java, you create a new Object like this:

Where we are importing the Scanner Class and any I/O classes that Java has. We create a new Object here from the Scanner Class by using Scanner keyboard = new Scanner(System.in);. This is an example of Classical inheritance, creating Objects from either predefined Classes in the Java API or Classes you create.

The syntax for creating Objects in JavaScript from Constructors, which is not the same as a Constructor function in Java, looks similar to creating an Object in Java.

Take a look:

Notice how we created the Objects: with the new keyword. Also notice that we have an instanceof operator. In Java, you create an instanceof a class by creating an Object through Classical inheritance. Here you’re creating an instanceof the constructor.prototype in the object’s prototype chain. This is weird and I am not sure why Prototypal inheritance wasn’t used.

On Treehouse, I am going through their Full-stack JavaScript track and we came up on OOP JavaScript.

Learning about Objects, state, and the origin and usage of this is intriguing as I have learned a bit about OOP through Java during my time at Pitt.

JavaScript doesn’t have regular Classes and instead uses Prototypes. Classical inheritance is seen as a big problem in JavaScript, which when you consider JavaScript doesn’t have real Classes, it makes sense. Brandon Eich, the creator of JavaScript admitted he wanted JavaScript to look like the little brother of Java, which leaves us with syntax that looks like Java when we use things like Constructors. For instance, in Java, you create a new Object like this:

Where we are importing the Scanner Class and any I/O classes that Java has. We create a new Object here from the Scanner Class by using Scanner keyboard = new Scanner(System.in);. This is an example of Classical inheritance, creating Objects from either predefined Classes in the Java API or Classes you create.

The syntax for creating Objects in JavaScript from Constructors, which is not the same as a Constructor function in Java, looks similar to creating an Object in Java.

Take a look:

Notice how we created the Objects: with the new keyword. Also notice that we have an instanceof operator. In Java, you create an instanceof a class by creating an Object through Classical inheritance. Here you’re creating an instanceof the constructor.prototype in the object’s prototype chain. This is weird and I am not sure why Prototypal inheritance wasn’t used.

I am reading a lot of Eric Elliott’s posts on the topic. It doesn’t jive with the stuff I am learning as it leans heavily on Prototypal Inheritance features in ES6. But I need to learn the OOP way first, at least for now, to lessen confusion.

© tiff.RSS