Created
September 12, 2020 09:34
-
-
Save Leonti/1ff89d1842b1e353b1576f2d1f626709 to your computer and use it in GitHub Desktop.
FP vs OOP classes
This file contains 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
==== FP === | |
function getFullName(first, second) { | |
return first + ' ' + second | |
} | |
const card1 = { | |
first: 'En Chi', | |
second: 'Chiang' | |
} | |
const card2 = { | |
first: 'Leonti', | |
second: 'Bielski' | |
} | |
console.log(getFullName(card1.first, card1.second)) | |
console.log(getFullName(card2.first, card2.second)) | |
==== OOP ==== | |
const card1 = { | |
first: 'En Chi', | |
second: 'Chiang', | |
getFullName: function() { | |
return this.first + ' ' + this.second | |
} | |
} | |
const card2 = { | |
first: 'Leonti', | |
second: 'Bielski', | |
getFullName: function() { | |
return this.first + ' ' + this.second | |
} | |
} | |
console.log(card1.getFullName()) | |
console.log(card2.getFullName()) | |
========== OOP2 ============== | |
function createCard(first, second) { | |
return { | |
first: first, | |
second: second, | |
getFullName: function() { | |
return this.first + ' ' + this.second | |
} | |
} | |
} | |
const card1 = createCard('En Chi', 'Chiang') | |
const card2 = createCard('Leonti', 'Bielski') | |
console.log(card1.getFullName()) | |
console.log(card2.getFullName()) | |
========== OOP3 =========== | |
class Card { | |
constructor(first, second) { | |
this.first = first | |
this.second = second | |
} | |
getFullName: function() { | |
return this.first + ' ' + this.second | |
} | |
} | |
const card1 = new Card('En Chi', 'Chiang') | |
const card2 = new Card('Leonti', 'Bielski') | |
console.log(card1.getFullName()) | |
console.log(card2.getFullName()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment