JavaScriptQuestion 21 of 36
What is the purpose of classes when we also have prototypes?
Answer out loud or in your head first, then compare.
Answer
JavaScript still uses prototypes.
class is mainly a more convenient syntax.
JavaScript
class User {
constructor(name) {
this.name = name;
}
sayHello() {
console.log(this.name);
}
}The method still lives on:
JavaScript
User.prototype.sayHelloClasses make these easier:
- the constructor,
- methods,
- inheritance,
super,- private fields,
- static methods.
How did it go? Be honest — “Not yet” puts the question on your review list.