Created
December 8, 2019 12:21
-
-
Save bhaveshdaswani93/84b06f7028c493f59488ca1fcc2733e1 to your computer and use it in GitHub Desktop.
This gist try to explain factory function
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
let personA = { | |
first_name: "lorem", | |
last_name: "ipsum", | |
getFullName() { | |
return `Full Name is: ${this.first_name} ${this.last_name}`; | |
} | |
}; | |
let personB = { | |
first_name: "rutrum", | |
last_name: "massa", | |
getFullName() { | |
return `Full Name is: ${this.first_name} ${this.last_name}`; | |
} | |
}; | |
// Here we are repeating getFullName method which is against DRY rule | |
// we can create a Factory function | |
let person = function(first_name,last_name) { | |
return { | |
first_name, | |
last_name, | |
getFullName() { | |
return `Full Name is: ${this.first_name} ${this.last_name}`; | |
} | |
} | |
} | |
let personC = person('erat','luctus'); | |
let personD = person('Quisque','vestibulum'); | |
// Now we are repeating our code as we have move our logic in the person function | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment