Skip to content

Instantly share code, notes, and snippets.

@BrianJenney
Created April 21, 2022 12:49
Show Gist options
  • Save BrianJenney/23f77cfe740eb8dc586dc775c70eabce to your computer and use it in GitHub Desktop.
Save BrianJenney/23f77cfe740eb8dc586dc775c70eabce to your computer and use it in GitHub Desktop.
/*
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