Created
October 5, 2018 02:57
-
-
Save StevenMDixon/6c6b7fd18f0c580d70fd851714bf67a5 to your computer and use it in GitHub Desktop.
Factory functions and what they do!
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
/* 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