목차
JS Closure
- 내부 함수에서 외부 함수의 지역변수에 접근하는 것을 의미
전역변수
- 해당 페이지 안이라면 어디서든 사용 가능
지역변수
- 함수 안에서 정의된 변수로써, 해당 함수 안에서만 사용 가능
JS Closure 예시
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
function Func01() { | |
const apple = "사과"; | |
return function Func02() { | |
const banana = "바나나";; | |
console.log(apple); | |
console.log(banana); | |
} | |
} | |
Func01()() |
┌ 스코프 체인에 의해 Func02() 함수 안의 apple은 함수 바깥에 있는 apple을 의미함
└ 따라서, 결과로 "사과", "바나나" 가 나옴
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
functino Func01 (apple) { | |
return function Func02(banana) { | |
console.llog("안녕 ?"); | |
console.log(apple); | |
console.log(banana); | |
} | |
} | |
Func01("사과")("바나나") |
┌ 마지막 줄의 Func01("사과")는 가장 윗줄에 있는 apple에 "사과"가 들어가게 되고,
├ 마지막 줄의 Func01(" ")("바나나")의 "바나나"는 Func02()의 banana에 들어가게 됨
└ 결과로 "안녕 ?", "사과", "바나나" 가 나옴
'React > 2022-上' 카테고리의 다른 글
React Currying (0) | 2022.04.18 |
---|---|
React HOC vs HOF (0) | 2022.04.18 |
권한 분기 (0) | 2022.04.18 |
Cookie, Session, Local Storage (0) | 2022.04.18 |
Regular Expression (정규표현식) (0) | 2022.04.17 |