chapter6. 함수

함수 : 하나의 단위로 실행되는 문의 집합

6.1 반환 값

return :함수를 즉시 종료하고 값을 반환
return을 명시적으로 호출하지 않으면 반환 값은 undefined가 됨

함수 return
1
2
3
4
5
function getGreeting() {
return "hello world";
}

getGreeting();

6.2 호출과 참조

함수 식별자 뒤에 ()를 쓰면 함수를 호출,
괄호를 쓰지 않으면 함수를 참조하며 함수 실행되지 않는다.

호출과 참조
1
2
3
4
5
6
7
function getGreeting() {
//함수 바디 중괄호
console.log("hello world");
}

getGreeting(); // "hello world";
getGreeting; // function getGreeting()
  • 변수에 할당
1
2
3
//변수에 할당
const f = getGreeting;
f(); //hello, world;
  • 객체 프로퍼티에 할당
1
2
3
4
//객체 프로퍼티에 할당
const o = {};
o.f = getGreeting;
o.f();
  • 배열 요소에 할당
1
2
3
4
//배열 요소에 할당
const arr = {1,2,3};
arr[1] = getGreeting;
arr[1]();

Comentarios

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×