22.1 this 키워드
this
- 자신이 속한 객체 또는 자신이 생성할 인스턴스를 가리키는 자기 참조 변수 (self-referencing variable)
- this를 통해 자신이 속한 객체 또는 자신이 생성할 인스턴스의 프로퍼티나 메서드를 참조할 수 있음
- this는 코드 어디에서든 참조 가능
this 바인딩
- this와 this가 가리킬 객체를 바인딩 (식별자와 값을 연결)
22.2 함수 호출 방식과 this 바인딩
- this 바인딩은 함수가 호출되는 방식에 따라 this 바인딩이 동적으로 결정
함수 호출 방식 | this 바인딩 |
일반 함수 호출 | 전역 객체 |
메서드 호출 | 메서드를 호출한 객체 |
생성자 함수 호출 | 생성자 함수가 (미래에)생성할 인스턴스 |
Function.prototype.apply/call/bind 메서드에 의한 간접 호출 | Function.prototype.apply/call/bind 메서드에 첫 번째 인수로 전달한 객체 |
22.2.1 일반 함수 호출
- 일반 함수로 호출된 모든 함수 (중첩 함수, 콜백 함수 포함) 내부의 this에는 전역 객체(global object)가 바인딩
- 매서드 내에서 정의한 중첩함수도 일반함수로 호출되면 중첩함수 내부의 this에는 전역 객체가 바인딩
//var 키워드로 선언한 전역 변수 value는 전역 객체의 프로퍼티
var value =1;
const obj = {
value: 100,
foo() {
console.log("foo's this:",this); //{ value: 100, foo: [Function: foo] }
console.log("foo's this.value:", this.value); //100
//메서드 내에서 정의한 중첩 함수
function bar() {
console.log("bar's this:",this);//window
console.log("bar's this.value:", this.value); //1
}
//메서드 내에서 정의한 중첩함수도 일반 함수로 호출되면
//중첩 함수 내부의 this에는 전역 객체가 바인딩
bar();
}
};
obj.foo();
- 콜백함수가 일반 함수로 호출된다면 콜백 함수 내부의 this에도 전역객체가 바인딩
var value = 1;
const obj = {
value : 100,
foo() {
console.log("foo's this: ", this); //{ value: 100, foo: [Function: foo] }
//콜백 함수 내부의 this에는 전역 객체가 바인딩
setTimeout(function() {
console.log("callback's this: ", this); //window
console.log("callback's this.value",this.value); //1
},100)
}
};
obj.foo();
- 외부 함수인 메서드와 중첩함수(또는 콜백함수)의 this가 일치하지 않는다는 것은 헬퍼함수의 역할의 동작을 어렵게 함
- 메서드 내부의 중첩함수(또는 콜백함수)의 this 바인딩을 메서드의 this 바인딩을 일치 시켜야 함
var value =1;
const obj = {
value : 100,
foo() {
//this 바인딩(obj)를 변수 that에 할당
const that = this;
//콜백 함수 내부에서 this 대신 that을 참조
setTimeout(function () {
console.log(that.value); //100
},100)
}
};
obj.foo();
22.2.2 메서드 호출
- 메서드 내부의 this에는 메서드를 호출할 때 메서드 이름 앞에 마침표(.)연산자 앞에 기술한 객체가 바인딩
- 메서드는 객체에 포함된 것이 아니라 독립적으로 존재하는 별도의 객체
- 메서드 내부의 this는 프로퍼티로 메서드를 가리키고 있는 객체와는 관계가 없고 메서드를 호출한 객체에 바인딩
- getName 프로퍼티가 가리키는 함수 객체 getName 메서드는 다른 객체의 프로퍼티에 할당하는 것으로 다른 객체의 메서드가 될 수도 있고 일반 변수에 할당하여 일반 함수로 호출될 수도 있음
const person = {
name:'Lee',
getName() {
//메서드 내부의 this는 메서드를 호출한 객체에 바인딩
return this.name;
}
};
//메서드 getName을 호출한 객체는 person
console.log(person.getName()); //Lee
const anotherPerson = {
name:'Kim'
};
//getName 메서드를 anotherPerson 객체의 메서드로 할당
anotherPerson.getName = person.getName;
//getName 메서드를 호출한 객체는 anotherPerson
//getName 메서드를 가리키는 객체는 person
console.log(anotherPerson.getName()); //Kim
//getName 메서드를 변수에 할당
const getName = person.getName;
//getName 메서드를 일반함수로 호출
console.log(getName()); // ' '
//일반함수로 호출된 getName 함수 내부의 this는 브라우저 환경에서 window.name과 같음
//기본값 ' '
22.2.3 생성자 함수 호출
- 생성자 함수 내부의 this에는 생성자 함수가 (미래에) 생성할 인스턴스에 바인딩
//생성자 함수
function Circle(radius) {
//생성자 함수 내부의 this는 생성자 함수가 생성할 인스턴스를 가리킴
this.radius = radius;
this.getDiameter = function() {
return 2* this.radius;
};
}
const circle1 = new Circle(5);
const circle2 = new Circle(10);
console.log(circle1.getDiameter()); //10
console.log(circle2.getDiameter()); //20
22.2.4 Function.prototype.apply/call/bind 메서드에 의한 간접 호출
- apply, call, bind 메서드는 Function.prototype의 메서드
- 모든 함수가 상속받아 사용 가능
- this로 사용할 객체와 인수 리스트를 인수로 전달받아 함수를 호출
1) apply, call 메서드
- 본질 기능은 함수를 호출하는 것
- 함수를 호출하면서 첫 번째 인수로 전달한 특정 객체를 호출한 함수의 this에 바인딩
- apply 메서드는 호출할 함수의 인수를 배열로 묶어 전달
- call 메서드는 호출할 함수의 인수를 쉼표로 구분한 리스트 형식으로 전달
function getThisBinding() {
console.log(arguments);
return this;
}
//this로 사용할 객체
const thisArg = {a:1};
console.log(getThisBinding.apply(thisArg, [1,2,3]));
//[Arguments] { '0': 1, '1': 2, '2': 3 }
//{ a: 1 }
console.log(getThisBinding.call(thisArg, 1,2,3));
//[Arguments] { '0': 1, '1': 2, '2': 3 }
//{ a: 1 }
- 활용 : argument 객체와 같은 유사배열 객체에 배열 메서드를 사용 하는 경우
function convertArgsToArray() {
console.log(arguments);
//arguments 객체를 배열로 변환
//Array.prototype.slice를 인수 없이 호출하면 배열의 복사본을 생성
const arr = Array.prototype.slice.call(arguments);
console.log(arr);
return arr;
}
convertArgsToArray(1,2,3); //[ 1, 2, 3 ]
2) bind 메서드
- 첫 번째 인수로 전달한 값으로 this 바인딩이 교체된 함수를 새롭게 생성해 반환
function getThisBinding() {
return this;
}
//this로 사용할 객체
const thisArg = {a:1};
//bind 메서든 첫번째 인수로 전달한 thisArg로 this 바인딩이 교체된
//getThisBinding 함수를 새롭게 생성해 반환
console.log(getThisBinding.bind(thisArg));
//bind메서드는 함수를 호출하지 않으므로 명시적으로 호출해야함
console.log(getThisBinding.bind(thisArg)()); //{ a: 1 }
- 활용 : 메서드의 this와 메서드 내부의 중첩 함수(또는 콜백함수)의 this가 불일치하는 문제 해결
const person = {
name : 'Lee',
foo(callback) {
//bind 메서드로 callback 함수 내부의 this 바인딩을 전달
setTimeout(callback.bind(this),100);
}
};
person.foo(function () {
console.log(`Hi! my name is ${this.name}.`); //Hi! my name is Lee.
});
'JavaScript' 카테고리의 다른 글
[Deep dive] 24장 클로저 (0) | 2023.08.14 |
---|---|
[Deep dive] 23장 실행 컨텍스트 (0) | 2023.08.14 |
[Deep dive] 21장 빌트인 객체 (0) | 2023.08.11 |
[Deep dive] 20장 strict mode (0) | 2023.08.11 |
[Deep dive] 19장 프로토타입 (3) (0) | 2023.08.11 |