⚠️thisArg
아래와 thisArg라는 콜백함수를 발견 할 수 있었지만 이해할 수 없었다
arr.findIndex(callback(element[, index[, array]])[, thisArg])
그래서 검색해 보았다
https://ktpark1651.tistory.com/215
thisArg 에 대한 이해 (feat. filter 메소드)
목차 개요 filter 메소드 설명 뭐가 궁금해? 마무리 출처 개요 'thisArg 가 뭐가 그렇게 중요해? 몰라도 filter 메서드 그냥 쓸 수 있는데' 라고 할 수 있다. 나도 그렇게 가볍게 생각했었다..공부를 하
ktpark1651.tistory.com
결국 위 글에서는 'thisArg 는 Optional 인자로, thisArg를 인자로 넘기면 runtime이 callback을 호출할 때 이를 this로 설정한다라고 정의한다 (이해불가)
function test() {
let result1, result2;
var arr3 = [
{ num: 0, name: "홍길동" },
{ num: 1, name: "박보검" },
{ num: 2, name: "강호동" },
];
function isPositibeNum(value) {
console.log(this); // Window객체를 반환한다.. ???
return value.num > 0;
}
const result3 = arr3.find(isPositibeNum);
console.log(result3); // { num: 1, name: "박보검" }, 조건에 맞는 첫번째 객체를 소환
return `완료`;
}
//thisArg에 this값 바꿔주기
function isPositibeNum(value) {
console.log(this);
return value.num > this;
}
const result3 = arr3.find(isPositibeNum, 0);
console.log(result3); //{ num: 1, name: "박보검" }, 조건에 맞는 첫번째 객체를 소환
string을 this로 전달하면 어떻게 될까?
console.log(typeof arr3[2].name); //string
function isPositibeNum(value) {
console.log(this); // ▶String {"강호동"} 0:"강" 1:"호" 2"동" 이라는 스트링 객체가 나온다
return value.name == this; //타입이 다르니 ==대소비교를 이렇게 해야 아래와같은 결과가 나온다
}
const result3 = arr3.find(isPositibeNum, "강호동");
console.log(result3); // {num: 2, name: '강호동'}
return `완료`;
여기서 string과 String 객체가 따로 있다는걸 깨닫고 간다.
'개발' 카테고리의 다른 글
MVC패턴 (0) | 2022.09.05 |
---|---|
객체 배열 중복 제거 (Map())/배열 중복 개수 출력(forEach) (0) | 2022.05.25 |
시퀄라이즈 테이블 옵션 설정 (0) | 2022.05.23 |
시퀄라이즈 데이터 타입 (0) | 2022.05.23 |
시퀄라이즈(sequalize)와 마리아디비(MariaDB) 외부 접속[실패] (0) | 2022.05.23 |