2021.04.28
소스코드: calculator.html
click(~~~, onClickNumber(‘n’));의 onClickNumber가 있는 자리는 함수자리이므로 “함수값으로 리턴된 값”이 와야한다! 추가로 onClickNumber()함수를 불러오면 undefined가 자동으로 도출되기 때문에 return에 함수형으로 정의해야하며, 중복제거시에 유용한 고차함수를 써서 더 줄이자!
2021.04.30 추가
예시:
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;
}
function test() {
let result = "";
if (a) {
if (!b) {
result = "c";
}
result += "b";
return result;
} else {
result = "a";
result += "b";
return result;
}
}
function test() {
let result = "";
if (!a) {
//기존조건에서 반대 조건으로 변경.
result = "a";
result += "b";
return result;
} else {
if (!b) {
result = "c";
}
result += "b";
return result;
}
}
function test() {
let result = "";
if (!a) {
result = "a";
result += "b";
return result; //이미 리턴이 있다.
} else {
if (!b) {
result = "c";
}
result += "b";
return result;
}
}
function test() {
let result = "";
if (!a) {
result = "a";
result += "b";
return result;
}
if (!b) {
result = "c";
}
result += "b";
return result;
}
-, *, /는 문자열이 저절로 숫자로 바뀌기 때문에 parseInt를 쓰지 않아도 된다.