Object Oriented Programming - General questions re true classes

I’m working through the OOP part of your JavaScript course. I have some bigger picture questions.

In learning about JavaScript classes and objects from other resources on the Internet, (1) I know that “true” JavaScript classes are relatively new addition to the language, (2) I know that they are syntactic sugar on top of what we are learning in the OOP course, (3) I know that classes are SIMILAR to “true” objects, and (4) I know that “true” classes have effectively “replaced” what we are learning in this part of the course.

In other words, my understanding is that both of the below code blocks are essentially the same under the hood.

Class declaration

class Dog {
  constructor(name, personality) {
    this.name = name.toLowerCase()
    this.personality = personality.toLowerCase();
  }
  introduce() {
    const name = this.name[0].toUpperCase() + this.name.slice(1)
    const personality = this.personality;
    return `Hi! My name is ${name}, and I am ${personality}!`;
  }
}

const spike = new Dog("sPIKe", "swEET")

console.log(spike.introduce()) // => "Hi! My name is ..."

Constructor function

function Dog(name, personality) {
  this.name = name.toLowerCase();
  this.personality = personality.toLowerCase();
  Dog.prototype.introduce = function() {
    const name = this.name[0].toUpperCase() + this.name.slice(1)
    const personality = this.personality;
    return `Hi! My name is ${name}, and I am ${personality}!`;
  }
}

const spike = new Dog("sPIKe", "swEET")

console.log(spike.introduce()) // => "Hi! My name is ..."

The questions are

  1. Is it the correct understanding that both of the above code blocks are essentially “the same”?
  2. Is it true that class declarations are now best practice?
  3. If so, why are we learning the “old way” (the constructor function)?
  4. Is it so that we are familiar with what is happening under the hood?
  5. Is it so that we know what we are looking at if we see function ClassName {...} in the wild?
  6. Or, is it because this course was developed before “true” classes were a thing?
  7. All of the above?
  8. Will the course be updated (to teach “the new way”) like your CSS/HTML course was?
  9. Is there a notice that I missed (e.g. “As of ES6 this course is now outdated …”)?

This isn’t really true. A class is one way to describe the basic template of an object.

Ehh, not really.

JavaScript was designed for prototype based OOP, but at this point it supports both prototypes and classes.

Would you describe classes as a special type of object? Or perhaps as abstractions of prototypes?

No

Not that either.

Classes are just one way to describe how to make and use an object

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.