Last active
March 13, 2019 14:25
-
-
Save munkacsitomi/ab849f4f243346d323b3888524262fca to your computer and use it in GitHub Desktop.
Factory function examples in JavaScript
This file contains hidden or 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
// dog example | |
const dog = () => { | |
const sound = 'Where are my testicles, Summer?'; | |
return { | |
talk: () => console.log(sound) | |
} | |
} | |
const snuffles = dog(); | |
snuffles.talk(); | |
// create user example | |
const createUser = ({ | |
userName = 'Anonymous', | |
avatar = 'anon.png' | |
} = {}) => ({ | |
userName, | |
avatar | |
}); | |
console.log( | |
// { userName: "echo", avatar: 'anon.png' } | |
createUser({ userName: 'echo' }), | |
// { userName: "Anonymous", avatar: 'anon.png' } | |
createUser() | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment