Skip to content

Instantly share code, notes, and snippets.

@Maccauhuru
Last active February 26, 2019 14:27
Show Gist options
  • Save Maccauhuru/9e1e49d100118fe46b01c65ed06fea63 to your computer and use it in GitHub Desktop.
Save Maccauhuru/9e1e49d100118fe46b01c65ed06fea63 to your computer and use it in GitHub Desktop.
ES6 Classes Example
//define Book as an object constructor function
class Book {
constructor(name,author,price){
this.name = name;
this.author = author;
this.price = price;
}
getBookCost(){
return `The book ${this.name} was written by ${this.author} and costs $${this.price}`;
}
}
const jspatterns = new Book('JavaScript Patterns','Stoyan Stefanov',12);
const masteringnodejs = new Book('Mastering Node.js','Sandro Pasquali',9);
//call getBookDetails method on each defined book
jspatterns.getBookCost();// "The book JavaScript Patterns was written by Stoyan Stefanov and costs $12"
masteringnodejs.getBookCost();// "The book Mastering Node.js was written by Sandro Pasquali and costs $9"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment