Skip to content

Instantly share code, notes, and snippets.

@bhaveshdaswani93
Created December 8, 2019 12:21
Show Gist options
  • Save bhaveshdaswani93/84b06f7028c493f59488ca1fcc2733e1 to your computer and use it in GitHub Desktop.
Save bhaveshdaswani93/84b06f7028c493f59488ca1fcc2733e1 to your computer and use it in GitHub Desktop.
This gist try to explain factory function
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