Last active
January 9, 2017 04:51
-
-
Save uzyn/62af885fe82e12fe28d6caa5a7a8a06f to your computer and use it in GitHub Desktop.
Which is the preferred or trendy method for JS at this moment?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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