Skip to content

Instantly share code, notes, and snippets.

@StevenMDixon
Created October 5, 2018 02:57
Show Gist options
  • Save StevenMDixon/6c6b7fd18f0c580d70fd851714bf67a5 to your computer and use it in GitHub Desktop.
Save StevenMDixon/6c6b7fd18f0c580d70fd851714bf67a5 to your computer and use it in GitHub Desktop.
Factory functions and what they do!
/* What is a factory method? A factory method is any function that creates an object
example 1:
function userFactory =(username, bio)=> ({user: username, about: bio});
this factory function takes in a a username and a bio and creates a new object with user and about keys. this is great if you want to
create a bunch of users.
const beth = userFactory("Beth", "Hi im Beth");
const bob = userFactory("Bob", "Hi im Bob");
our objects can even have functions Assigned to them since they are object literals.
const animalFactory = (AnimalName, Noise) => ({
Name: AnimalName,
Noise,
speak(){
return `I am a ${Name} and I say ${this.noise}`;
}
});
we can also destructure arrays and objects
const swap = ([first, second]) => ([second, first]);
And compute the value of keys
const arrToObj = ([first, second]) => ({[first]: second})
These are fun But I haven't seen them used much
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment