Created
April 21, 2022 12:49
-
-
Save BrianJenney/23f77cfe740eb8dc586dc775c70eabce to your computer and use it in GitHub Desktop.
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
/* | |
a closure gives you access to an outer function's scope from an inner function. | |
*/ | |
const adder = (initialNum) => { | |
return(nextNum) => { | |
return initialNum + nextNum | |
} | |
} | |
const add2 = adder(2) | |
console.log(add2(2)) | |
const add4 = adder(4) | |
console.log(add4(4)) | |
const funcWithPrivateVars = () => { | |
//declared in closure scope | |
const superSecret = 'SECRET' | |
return{ | |
getSecret: () => { | |
return superSecret | |
} | |
} | |
} | |
const privateObj = funcWithPrivateVars() | |
console.log(privateObj.getSecret()) | |
//console.log(superSecret) -> will return undefined | |
const stephCurry = (num1) => { | |
//num1 | |
return (num2) => { | |
//num 2 | |
return (num3) => { | |
//num 3 | |
return num1 + num2 + num3 | |
} | |
} | |
} | |
console.log(stephCurry(1)(2)(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment