Skip to content

Instantly share code, notes, and snippets.

@Leonti
Created September 12, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leonti/1ff89d1842b1e353b1576f2d1f626709 to your computer and use it in GitHub Desktop.
Save Leonti/1ff89d1842b1e353b1576f2d1f626709 to your computer and use it in GitHub Desktop.
FP vs OOP classes
==== 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