함수
const f = function() {
console.log(`hi there + ${x} + ${p}`)
console.log(`hi there + ${x} + ${p}`)
console.log(`hi there + ${x} + ${p}`)
}
const x= 10
f()
const p= 20
// 함수() = 함수호출 >> 함수의 본문(console.log()등등 실행)
// x를 넣고 f()함수 호출. p는 함수 호출을 하고 난 뒤라 입력이 안됨. 그리고 p가 undefined 되었다고 오류를 리턴.
프로시저.
매개변수를 갖지않는 함수를 프로시저라고 한다.
위의 f()
가 프로시저.
수학적 함수.
const f = function(x/*매개변수*/) {
return x+5 // 리턴값
}
const sum = function (hhh) {
let output = 0
for (let i = 1; i <= hhh; i++) {
output += i
}
return output
}
console.log(`합은 ${sum(5)}`)
익명함수와 선언적 함수
//익명함수
const f = function (매개변수, 매개변수) {
return 리턴값
}
//선언적 함수
function f (매개변수, 매개변수) {
return 리턴값
}
윤년구하기.
const isLeapYear = function (연도) {
return (연도 % 4 === 0) && (연도 % 100 === 0) || (연도 % 400 === 0)
} // true / false 값 반환
최소값 찾기.
const min = function(배열) {
let output = 배열[0]
//console.log(`처음 시작 output = ${output}`)
for (const value of 배열) {
if(output > value) {
//console.log(`비교 중 입니다. ${output} vs ${value}`)
output = value;
}
//console.log(`= ${output}`)
}
return output
}
console.log(min([52, 273, 32, 103, 275, 24, 57]))
API.
Application Programming Interface의 약자.
어플리케이션 프로그램을 만들 때의 약속.
나머지 매개변수.
rest parameter.
const 함수 = function (...매개변수) {
console.log(매개변수) //
}
const f = function(...para) {
console.log(para)
}
f() // []
f(1) // [1]
f(1,2) // [1,2]
f(1,2,3,4) // [1,2,3]
const f = function(a,b,...para) {
console.log(a,b, para)
}
f() // undefined undefined []
f(1) // 1 undefined []
f(1,2) // 1 2 []
f(1,2,3,4) // 1 2 [3,4]
※참고사항.
const f = function (...para, a, b)
는 사용 할 수 없다. 나머지 어디까지가 나머지 매개변수인지 확인할 수 없기 때문.
전개 연산자.
함수를 호출 할 때, 배열 복사, 객체 복사를 할 때도 사용.
const 함수 = function(a,b,c) {
console.log(a, b, c)
}
const a = [1, 2, 3]
함수(a[0], a[1], a[2]) // 1 2 3
함수(...a) // 1 2 3
활용?
const max = function(...배열) { // 여기 바꾸는거 중요!!!
let output = 배열[0]
console.log(`처음 시작 output = ${output}`)
for (const value of 배열) {
if(output < value) {
console.log(`비교 중 입니다. ${output} vs ${value}`)
output = value;
}
console.log(`= ${output}`)
}
return output
}
const a = [52, 273, 32, 103, 275, 24, 57]
console.log(max(...a)) // 전개연산자
※매개변수의 자료형에 따라 다르게 작동하는 min()함수
function min(first, ...rests) {
//변수 선언
let output
let items
// 매개변수의 자료형에 따라 조건 분기하기
if (Array.isArray(first)){
output = first[0]
items = first
} else if (typeof(first) === 'number') {
output = first
items = rests
}
//이전 절에서 살펴보았던 최솟값을 구하는 공식
for (const item of items) {
if (output > item) {
output = item
}
}
return output
}
console.log(`min(배열): ${min([52, 273, 32, 103, 275, 24, 57])}`)
console.log(`min(숫자, ...): ${min(52, 273, 32, 103, 275, 24, 57)}`)
// 어떤 자료가 배열인지 확일 할 때는 Array.isArray() 메서드를 사용. 일반적인 typeof 연산자로는 배열을 확인할 수 없음.
기본 매개변수.
요즘 많이 쓰는 기본 매개변수 코드.
const isLeapYear = function (연도 = new Date().getFullYear()) { // 기본 매개변수
console.log(`${연도}`)
return (연도 % 4 === 0) && (연도 % 100 === 0) || (연도 % 400 === 0)
} // true / false 값 반환
console.log(isLeapYear()) // 함수에 값을 넣지않아도 실행이 된다.
옛날에 많이 쓰던 매개변수 코드.
const isLeapYear = function (연도) {
연도 = typeof(연도) === 'undefined' ? new Date.getFullYear() : 연도 // 연도의 타입이 unde이면 getFullyear을 넣어라. 아니면 연도의 값을 그대로 연도에 넣어라.
연도 = typeof(연도) !== 'undefined' ? 연도 : new Date.getFullYear() // 부정연산자를 써서 순서 바꿔줌.
console.log(`연도: ${연도}`)
return (연도 % 4 === 0) && (연도 % 100 === 0) || (연도 % 400 === 0)
} // true / false 값 반환
낭가피터