겁니다. 하지만 프로그램을 실제 실행하면 실행 흐름은 이곳 저곳으로 움직입니다. 함수 두 개
를 쓰는 프로그램이 있다고 합시다.
function
f1
() {
console
.
log
('
one
');
}
function
f2
() {
console
.
log
('
two
');
}
f2
();
f1
();
f2
();
정적으로 보면 이 프로그램은 단순히 위에서 아래로 읽어내리는 문의 연속입니다. 하지만 이
프로그램을 실행하면 실행 흐름은 읽는 순서와 다릅니다.
f1
이
f2
보다 먼저 정의됐지만,
f2
의
함수 바디가 실행된 다음
f1
으로, 다시
f2
로 넘어갑니다.
자바스크립트의 스코프는
정적
입니다. 소스 코드만 봐도 변수가 스코프에 있는지 판단할 수 있
다는 뜻입니다. 다만, 소스 코드만 봐도 즉시 스코프를
분명히
알 수 있다는 뜻은 아닙니다. 이
장에서 꼼꼼히 살피다 보면 스코프를 판단할 수 있는 몇몇 예제를 찾아볼 수 있을 겁니다.
정적 스코프는 어떤 변수가 함수 스코프 안에 있는지 함수를
정의할 때
알 수 있다는 뜻입니다.
호출할 때
알 수 있는 것은 아닙니다. 다음 예제를 보십시오.
const
x
=
3
;
function
f
() {
console ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.