About Lesson
The this Keyword
In JavaScript classes, this refers to the instance of the class. It’s used to access an object’s properties and methods.
Example: Using this in a Class
JavaScript
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
describe() {
console.log(`This book is titled '${this.title}' by ${this.author}.`);
}
}
const myBook = new Book("1984", "George Orwell");
myBook.describe(); // Output: This book is titled '1984' by George Orwell.
In this example, this.title and this.author refer to the myBook object’s title and author properties, allowing the describe method to display this information.