Skip to content

Instantly share code, notes, and snippets.

@Maccauhuru
Created February 11, 2019 01:11
Show Gist options
  • Save Maccauhuru/5c73feba940f3d3b09677cc4ccb51177 to your computer and use it in GitHub Desktop.
Save Maccauhuru/5c73feba940f3d3b09677cc4ccb51177 to your computer and use it in GitHub Desktop.
Object Method ES5
//define Book as an object constructor function
function Book(name,author) {
this.name = name;
this.author = author;
}
//add getBookDetails method to object prototype
Book.prototype.getBookDetails = function (){
return `The book ${this.name} was written by ${this.author}`;
}
//add some new books
const jspatterns = new Book('JavaScript Patterns','Stoyan Stefanov');
const masteringnodejs = new Book ('Mastering Node.js','Sandro Pasquali');
//call getBookDetails method on each defined book
jspatterns.getBookDetails();// "The book JavaScript Patterns was written by Stoyan Stefanov"
masteringnodejs.getBookDetails();// "The book Mastering Node.js was written by Sandro Pasquali"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment