2021.04.28
1. 고차함수
2. event
3. 중첩 조건문 해결하는 법
$result.value += "0";
click(~~~, onClickNumber(‘n’));의 onClickNumber가 있는 자리는 함수자리이므로 “함수값으로 리턴된 값”이 와야한다!
추가로 onClickNumber()함수를 불러오면 undefined가 자동으로 도출되기 때문에 return에 함수형으로 정의해야하며, 중복제거시에 유용한 고차함수를 써서 더 줄이자!
예시:
const fuc = (msg) => {
return () => {
console.log(msg);
};
};
중괄호와 리턴을 생략할 수 있으니 요놈이
const fuc = (msg) => () => {
console.log(msg);
};
이렇게 바뀌는데 이러한 코드를 보면 항상 머리로만 하지 말고 직접 쳐보고 비교해보는 버릇을 기르자. 원리는 이렇다.
const fuc =(msg) => "{"
"return"() =>{
console.log(msg);
}
"}"
위와 같이 쌍따옴표(““)로 묶은 부분을 제거한거다.
click시 함수가 호출되면 브라우저가 onClickNumber()
식으로 함수를 실행하는데, 그때 인자값으로 event
를 넣어주는데 그때 함수에 event
가 전달된다.
function test() {
let result = "";
if (a) {
if (!b) {
result = "c";
}
} else {
result = "a";
}
result += "b";
return result;
}
‘if문 다음에 나오는’ 공통된 절차를 각 분기점 내부에 넣는다.
function test() {
let result = "";
if (a) {
if (!b) {
result = "c";
}
result += "b";
return result;
} else {
result = "a";
result += "b";
return result;
}
}
분기점에서 ‘짧은 절차’부터 실행하게 if문을 작성한다.
function test() {
let result = "";
if (!a) {
//기존조건에서 반대 조건으로 변경.
result = "a";
result += "b";
return result;
} else {
if (!b) {
result = "c";
}
result += "b";
return result;
}
}
짧은 절차가 끝나면 return
이나 break
로 중단한다.
function test() {
let result = "";
if (!a) {
result = "a";
result += "b";
return result; //이미 리턴이 있다.
} else {
if (!b) {
result = "c";
}
result += "b";
return result;
}
}
else
를 제거한다.(이때 중첩 하나가 제거된다.)
function test() {
let result = "";
if (!a) {
result = "a";
result += "b";
return result;
}
if (!b) {
result = "c";
}
result += "b";
return result;
}
다음 중첩된 분기점이 나올 때 1~4의 과정을 반복한다.
parseInt
를 쓰지 않아도 된다.