Search⌘ K
AI Features

Solution Review: ES6 Classes

Explore how to convert traditional constructor functions to ES6 class syntax in JavaScript. Understand the importance of defining methods within class bodies to ensure proper inheritance and avoid runtime errors. This lesson helps you confidently use ES6 classes to implement object-oriented concepts.

We'll cover the following...

Solution

Javascript (babel-node)
class Cat {
constructor (name) {
this.name = name
}
meow () {
console.log(this.name + ' says meow')
}
}
let catty = new Cat('catty')
catty.meow()

Explanation

This challenge tests your knowledge of OOP in the ES6 version of JavaScript.

The following code was provided to you:

Javascript (babel-node)
function Cat (name) {
this.name = name
}
Cat.meow = function () {
console.log(this.name + ' says meow')
}
let catty = new Cat('catty')
catty.meow()

Let’s figure out what the issue in this code was. From lines 1-3 ...