Skip to content

Instantly share code, notes, and snippets.

@uzyn
Last active January 9, 2017 04:51
Show Gist options
  • Save uzyn/62af885fe82e12fe28d6caa5a7a8a06f to your computer and use it in GitHub Desktop.
Save uzyn/62af885fe82e12fe28d6caa5a7a8a06f to your computer and use it in GitHub Desktop.
Which is the preferred or trendy method for JS at this moment?
/**
* Method A: OOP
*/
class Library {
constructor() {
this.books = this.loadBooks();
}
add(newBook) {
this.books.push(newBook);
}
loadBooks() {
return api.books(); // array
}
}
module.exports = new Library(); // singleton
// -------------------------------------------------------------------------------------
/**
* Method B: Functional, with state
*/
const books = loadBooks();
function loadBooks() {
return api.books(); // array
}
function add(newBook) {
books.push(newBook);
}
module.exports = { books, add };
// -------------------------------------------------------------------------------------
/**
* Method C: Fully functional
*/
function getBooks() {
return api.books(); // array
}
function add(books, newBook) {
books.push(newBook);
return books;
}
module.exports = { getBooks, add };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment