DeveloperBreeze

Object-Oriented Programming Development Tutorials, Guides & Insights

Unlock 2+ expert-curated object-oriented programming tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your object-oriented programming skills on DeveloperBreeze.

Understanding JavaScript Classes

Tutorial September 02, 2024
javascript

Let's put everything together by building a simple application using classes. We'll create a Book class and a Library class to manage a collection of books.

class Book {
  constructor(title, author, isbn) {
    this.title = title;
    this.author = author;
    this.isbn = isbn;
  }

  getDetails() {
    return `${this.title} by ${this.author} (ISBN: ${this.isbn})`;
  }
}

class Library {
  constructor() {
    this.books = [];
  }

  addBook(book) {
    this.books.push(book);
  }

  removeBook(isbn) {
    this.books = this.books.filter(book => book.isbn !== isbn);
  }

  listBooks() {
    return this.books.map(book => book.getDetails()).join('\n');
  }
}

const library = new Library();
const book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', '9780743273565');
const book2 = new Book('1984', 'George Orwell', '9780451524935');

library.addBook(book1);
library.addBook(book2);
console.log(library.listBooks());
// Output:
// The Great Gatsby by F. Scott Fitzgerald (ISBN: 9780743273565)
// 1984 by George Orwell (ISBN: 9780451524935)

PHP Class and Object Example

Code January 26, 2024
php

No preview available for this content.