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