-
자바스크립트 call, apply, bindJavaScript 2023. 2. 7. 22:07
call, apply, bind : this를 지정, 함수호출 방식과 관계없다.
const kwak = { name : "kwak", }; function showName() { console.log(this.name); } showName(); showName.call(kwak);객체 수정
- apply는 배열을 사용하는 것이 call과 다르다.
function updateObj(age, gender) { this.age = age; this.gender = gender; } updateObj.call(kwak, 30, "M"); // updateObj.apply(kwak, [30, "M"]); console.log(kwak);bind : 함수의 this값을 영구히 바꿀 수 있다.
const bindObj = {name : "kwak"}; const updateBindObj = updateObj.bind(bindObj); updateBindObj(30, M); console.log(bindObj);활용
const ex = { name : "ex", show : function() { console.log(`welcome, ${this.name}`); }, }; ex.show(); let exFn1 = ex.show; exFn1.call(ex); exFn1.apply(ex); let exFn2 = exFn1.bind(ex); exFn2();'JavaScript' 카테고리의 다른 글
자바스크립트 __proto__ / prototype 차이 (0) 2023.02.07 자바스크립트 prototype chain 상속 (0) 2023.02.07 자바스크립트 setTimeout, setInterval, clearTimeout, clearInterval (0) 2023.01.30 자바스크립트 함수, 함수 호출의 차이 (0) 2022.11.24 자바스크립트 배열, 배열 메서드 (0) 2022.11.24