마지막 예제를 보면 자바스크립트에서 괄호가 하는 일을 명확히 알 수 있을 겁니다. 값 뒤에 괄
호를 붙이면 자바스크립트는 그 값을 함수로 간주하고 호출합니다. 이전 예제에서
arr
[
1
]
은
값이 되는 표현식입니다. 값 뒤에 괄호를 썼으므로 자바스크립트는 이 값이 함수라고 간주하고
호출합니다.
함수가 아닌 값 뒤에 괄호를 붙이면 에러가 일어납니다. 예를 들어 "
whoops
"()는
TypeError
:
"
whoops
"
is
not
a
function
에러를 일으킵니다.
6.3
함수와 매개변수
함수를 호출해서 값을 얻는 방법은 배웠습니다. 그렇다면 함수에 정보를
전달하려면
어떻게 해
야 할까요? 함수를 호출하면서 정보를 전달할 때는 함수
매개변수
argument
,
parameter
를 이용합니
다. 매개변수는 함수가 호출되기 전에는 존재하지 않는다는 점을 제외하면 일반적인 변수나 마
찬가지입니다. 숫자형 매개변수 두 개를 받고 그 평균을 반환하는 함수를 생각해 봅시다.
function
avg
(
a
,
b
) {
return
(
a
+
b
)/
2
;
}
이 함수 선언에서
a
와
b
를
정해진 매개변수
formal
argument
라고 합니다. 함수가 호출되면 정해진
매개변수는 값을 받아
실제 매개변수
actual
argument
가 됩니다.
avg
(
5
,
10
); //
7
.
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.