함수 작성 방법 - 선언식
function 함수명(...){
코드
}
ex)
function hello(a,b){
return a+b;
}
console.log(hello(3,4));
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function line(cnt,type){
for(i=0; i<cnt; i++){
document.write(type)*i;
}
document.write("<br>");
}
line(40,'-');
document.write(" 고객 관리 프로그램 <br>")
line(30,'=');
document.write("1. 등록 <br>");
document.write("2. 검색 <br>");
line(50,'*');
</script>
</head>
<body>
</body>
</html>
함수 작성 방법 - 표현식
--> 무명함수
변수 = function() {코드}
변수 () ;
ex)
function hello(){
console.log("Hello~");
}
hello();
--> 위의 코드를 무명함수로 바꾸기
const hello = function(){
console.log("Hello~");
}
함수 작성 방법 - 화살표 함수
변수 = { } => {return 코드;}
ex)
/ 함수 표현식 /
const hello = function(a,b){
return a+b;
}
console.log(hello(3,4));
--> 위의 표현식을 화살표 함수를 사용하기
const hello = (a,b) => {return a+b;}
console.log(hello(3,4));
/ 화살표 함수 /
const hello = (a, b) => { return a+b; }
console.log(hello(2,3));
함수 호출 방식
인자가 있는 방식
function line(cnt){
실행할코드
}
line(숫자); // 함수에 인자를 넣어준다
-->
이때, 숫자를 '실인자(argument)' 라고 부르고
function에 정의한 cnt는 매개변수(parameter)라 부른다
function line(cnt, type){
실행할코드
}
line(숫자, '지정할타입');
-->
함수를 사용하여 나타낼 line의 type까지 지정가능
return 값이 존재하는 경우
- 값을 반환 ( 반드시 1개만 리턴 가능 )
- 함수를 강제로 종료 (원위치로 돌아감)
ex)
function hap(n1,n2){
if(n1 == " "){
return;
}
document.write("<h2>두 수의 합계</h2>");
let num3 = n1 + n2;
return num3;
}
호이스팅 ( Hoisting )
ex)
hello();
function hello() {
console.log("hello~");
}
-->
원래 같으면 function 을 정의하고 하단에 함수를 호출하는데, 함수 호출을 상위에서 호출하는 이 방식을 호이스팅 이라고 함
ex)
hello();
hello = function() {
console.log("hello~world");
}
hello();
-->
무명함수를 실행할 경우 호이스팅 적용 불가능
Default Value
fuction sum(a,b,c){
return a+b+c;
}
console.log(sum(1,2,3));
--> default value 값 이용한 코드
function sum(a,b,c=3){
return a+b+c;
}
console.log(sum(1,2));
**
default value 값을 정의했지만 console.log에서 다시 정의한 경우
--> console.log 에서 정의한 c=4 의 값이 적용 !
function sum(a,b,c=3){
return a+b+c;
}
console.log(sum(1,2,4));
객체의 구조 분해 할당
--> 객체를 분해하여 그 값을 변수에 할당
const user = {name : "홍길동", age : 35}
function getName(u){
return user.name;
}
console.log{getName(user));
function getName(u){
const {name} = u;
return name;
}
console.log(getName(user));
function getName({name}){
return name;
}
console.log(getName(user));
ex) getmail 함수의 매개변수로 전달된 객체의 email 속성값을 가져오거나 해당 속성이 없으면 "이메일이 없습니다" 를 기본값으로 설정하여 반환
function getEmail({emaile="이메일이 없습니다"}){
return email;
}
console.log(getEmail(user));
배열의 구조 분해 할당
--> 배열을 분해하여 그 값을 변수에 할당
const fruit = ['Apple', 'Banana', 'Cherry'];
function getSecondItem(data){
return data[1];
}
console.log(getSecondItem(fruit));
const fruit = ['Apple', 'Banana', 'Cherry'];
funciton getSecondItem([a,b,c]){
return b;
}
console.log(getSecondItem(fruit));
const fruit = ['Apple', 'Banana', 'Cherry'];
function getSecondItem([,b]){
return b;
}
console.log(getSecondItem(fruit))
rest 매개변수
--> 갯수와 상관없이 실 인자값을 다 받아줄 수 있음
--> (...rest) 로 표현
코드 작성
const a = x => {
return x*x}
--> return 을 생략한다면 {} 도 생략해야 함 !
const a = x => x*x
***
코드가 여러줄일때는 {} 와 return 생략 불가능
const = x => {
console.log(x*x);
return x*x;
}
객체 사용
return 사용
const a = () => {return {x:1}}
return 사용 X --> () 사용해야 함 !
const a = () => ({x:1})
오류발생
const a = () => {x:1}
배열 사용
const a = () => {return [1,2,3]}const a = () => [1,2,3]--> 함수가 실행될때 인자로 들어가는 또 다른 함수
--> 함수 내에서 자신을 호출
function a() {
...
a();
}
a();