Last active
May 30, 2019 15:13
-
-
Save hossainlab/60fe90cdde804567fa3e7b539ee70fc4 to your computer and use it in GitHub Desktop.
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
| // JavaScript OOP Concepts | |
| /* | |
| Concept-1:Object Literals | |
| Concept-2:Constructor & This | |
| Concept-3:Prototypes & Inheritence | |
| Concept-4:Classes and Subclasses | |
| */ | |
| // Concept-1:Object Literals | |
| const bookOne = { | |
| title:'Book One', | |
| author:'Jubayer Hossain', | |
| year:'2018', | |
| getSummary:function() { | |
| return `${this.title} was written by ${this.author} in ${this.year}` | |
| } | |
| }; | |
| console.log(bookOne.title); | |
| console.log(bookOne.getSummary()); | |
| const bookTwo = { | |
| title:'Book Two', | |
| author:'Tasmim Rahman', | |
| year:'2013', | |
| getSummary:function() { | |
| return `${this.title} was written by ${this.author} in ${this.year}` | |
| } | |
| }; | |
| // Values and Keys | |
| console.log(Object.values(bookTwo)); | |
| console.log(Object.keys(bookTwo)); | |
| // Concept-2:Constructor & This | |
| function Book(title, author, year) { | |
| this.title = title; | |
| this.author = author; | |
| this.year = year; | |
| this.getSummary = function() { | |
| return `${this.title} was written by ${this.author} in ${this.year}` | |
| }; | |
| } | |
| // Instantiate an object | |
| const bookOne = new Book( | |
| 'Book One', | |
| 'Jubayer Hossain', | |
| '2015' | |
| ); | |
| const bookTwo = new Book( | |
| 'book Two', | |
| 'Tasmim Rahman', | |
| '2016' | |
| ); | |
| console.log(bookOne); | |
| console.log(bookTwo); | |
| console.log(bookOne.getSummary()); | |
| console.log(bookTwo.getSummary()); | |
| // Concept-3:Prototypes & Inheritence | |
| function Book(title, author, year) { | |
| this.title = title; | |
| this.author = author; | |
| this.year = year; | |
| } | |
| // getSummary | |
| Book.prototype.getSummary = function() { | |
| return `${this.title} was written by ${this.author} in ${this.year}` | |
| }; | |
| // Magazine Constructor | |
| function Magazine(title, author, year, month) { | |
| Book.call(this, title, author, year); | |
| this.month = month; | |
| } | |
| // Inheritence | |
| Magazine.prototype = Object.create(Book.prototype); | |
| const MagOne = new Magazine( | |
| 'Mag One', | |
| 'Tasmim Adib', | |
| '2017', | |
| 'Jan' | |
| ); | |
| // Use Magazine constructor | |
| Magazine.prototype.constructor = Magazine; | |
| console.log(MagOne.getSummary()); | |
| console.log(MagOne); | |
| // Concept-4:Classes and Subclasses | |
| class Book { | |
| constructor(title, author, year) { | |
| this.title = title; | |
| this.author = author; | |
| this.year = year; | |
| } | |
| getSummary() { | |
| return `${this.title} was written by ${this.author} in ${this.year}` | |
| }; | |
| getAge() { | |
| const years = new Date().getFullYear() - this.year; | |
| return `${this.title} is ${years} years old!` | |
| } | |
| revise(newYear) { | |
| this.year = newYear; | |
| this.revised = true; | |
| } | |
| static topBookStore() { | |
| return 'Story & Nobles' | |
| } | |
| } | |
| // Instantiate Object | |
| const bookOne = new Book('Book One', 'Tasmim Adib', '2014'); | |
| console.log(bookOne) | |
| console.log(bookOne.getSummary()); | |
| console.log(bookOne.getAge()); | |
| ! Revise | |
| console.log(bookOne); | |
| bookOne.revise('2018'); | |
| console.log(bookOne); | |
| // static method | |
| console.log(Book.topBookStore()); | |
| // Magazine Subclasses | |
| class Magazine extends Book { | |
| constructor (title, author, year, month) { | |
| super(title, author, year); | |
| this.month = month; | |
| } | |
| } | |
| const magOne = new Magazine('Magazine One','Tasmim Adib', '2014', 'Jan'); | |
| const magTwo = new Magazine('Magazine Two','Tasmim Rahman', '2012', 'Feb'); | |
| // console.log(magOne.getSummary()); | |
| // console.log(magOne.getAge()); | |
| console.log(magOne); | |
| console.log(magTwo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment