클로저
function outerFunction(arg) {
var variableInOuterFunction = arg
function bar() {
console.log(variableInOuterFunction) // Access a variable from the outer scope
}
// Call the local function to demonstrate that it has access to arg
bar()
}
outerFunction('hello closure') // logs hello closure!function outerFunction(arg) {
var variableInOuterFunction = arg
return function() {
console.log(variableInOuterFunction)
}
}
var innerFunction = outerFunction('hello closure!')
// Note the outerFunction has returned
innerFunction() // logs hello closure!클로저가 엄청난 이유
Last updated