๐กthis๋?
ํจ์๊ฐ ํธ์ถ๋ ๋ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ ํน๋ณํ ํค์๋๋ก '๋๊ฐ ๋๋ฅผ ๋ถ๋ ๋๋'๋ฅผ ๋ปํ๋ค๊ณ ํ๋ค ์ฆ, ์ ์ธ์ด ์๋ ํธ์ถ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ค
this๋ ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์(self-reference variable)๋ก ์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
๐งฉ๋จ๋ ์ผ๋ก ์ด this
์ ์ญ ์คํ์ปจํ ์คํธ์ this, ์ฆ ์๋ฌด ํจ์์๋ ์ํ์ง ์์ ๋ฒ์์์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ค
console.log(this); // window ๊ฐ์ฒด ์ถ๋ ฅ
๐งฉํจ์ ์์์ ์ด this
ํจ์ ์์์ this๋ ํจ์์ ์ฃผ์ธ์๊ฒ ๋ฐ์ธ๋ฉ๋๋ค =>window๊ฐ์ฒด
โ ์ผ๋ฐ์ ์ธ This
function myFunction() {
return this;
}
console.log(myFunction()); //Window
โ strict mode This
"use strict";
function myFunction() {
return this;
}
console.log(myFunction()); //undefined
๐งฉ๋ฉ์๋ ์์์ ์ด this
๋ฉ์๋ ํธ์ถ ์ ๋ฉ์๋ ๋ด๋ถ ์ฝ๋์์ ์ฌ์ฉ๋ this๋ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉ๋๋ค
var person = {
firstName: 'Kim',
lastName: 'Sora',
fullName: function () {
return this.firstName + ' ' + this.lastName;
},
};
person.fullName(); //"Sora Kim"
๐งฉ์ด๋ฒคํธ ํธ๋ค๋ฌ ์์์ ์ด this
์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ DOM ์์๋ฅผ ์ฐธ์กฐํ๋ค=>์ด๋ฒคํธ๋ฅผ ๋ฐ๋ HTML ์์๋ฅผ ๊ฐ๋ฆฌํจ๋ค
<button id="myButton">Click me</button>
const myButton = document.querySelector("#myButton");
myButton.addEventListener("click", function() {
console.log(this);
}); // myButton ์์ ์ถ๋ ฅ
๐งฉ๋ช ์์ ์ผ๋ก this๋ฅผ ์ง์ ํ๋ ๋ฐฉ๋ฒ =>call, apply, bind ๋ฉ์๋๋ฅผ ์ฌ์ฉ
โ๏ธcall
๋ฉ์๋ ํธ์ถ ์ฃผ์ฒด์ธ ํจ์๋ฅผ ์ฆ์ ์คํํ๋ ๋ฉ์๋
func.call(thisArg[, arg1[, arg2[, ...]]])
let person1 = {
name: 'Koding'
};
let person2 = {
name: 'Kim',
study: function() {
console.log(this.name + '์ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.');
}
};
person2.study(); //Kim์ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
// call()
person2.study.call(person1);//Koding์ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
person2.study()๋ฅผ ํธ์ถํ๋ฉด person2 ๊ฐ์ฒด์ study ๋ฉ์๋๊ฐ ์คํ๋๋ฉฐ, this.name์ผ๋ก person2 ๊ฐ์ฒด์ name ํ๋กํผํฐ ๊ฐ์ธ 'Kim'์ด ์ถ๋ ฅ๋๋ค
call ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ person2.study ๋ฉ์๋๋ฅผ person1 ๊ฐ์ฒด์์ ํธ์ถํ๋ฉด, study ๋ฉ์๋ ๋ด๋ถ์์ this.name์ผ๋ก ์ฐธ์กฐ๋๋ this๊ฐ person1 ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก, 'Koding'์ด ์ถ๋ ฅ๋๋ค
โ๏ธApply
๋ฉ์๋ ํธ์ถ ์ฃผ์ฒด์ธ ํจ์๋ฅผ ์ฆ์ ์คํํ๋ ๋ฉ์๋
argsArray: func์ด ํธ์ถ๋์ด์ผ ํ๋ ์ธ์๋ฅผ ์ง์ ํ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด
๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด ํํ๋ก ๋ฃ๊ฒ ๋๋ค๋ ์ฐจ์ด์ ์ด ์๋ค(๋ฐฐ์ด ๋๋ ์ ์ฌ๋ฐฐ์ด ๊ฐ์ฒด)
fun.apply(thisArg, [argsArray])
function add(c, d) {
return this.a + this.b + c + d;
}
let A = {a: 3, b: 3};
add.call(A, 6, 6); // 18
add.apply(A, [20, 10]); // 36
call์ ์ธ์๋ฅผ ๊ฐ๊ฐ ์ ๋ฌํ๋ ๋ฐฉ์์ด๊ณ , apply๋ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ์ ๋ฌํ๋ ๋ฐฉ์์ด๋ค
๊ฐ์ฒด A๋ฅผ this๋ก ์ค์ ํ๋ฉด์ add ํจ์๋ฅผ ํธ์ถํ๊ณ , call์ ์ธ์๋ฅผ ๊ฐ๊ฐ ์ ๋ฌํ๊ณ apply๋ ๋ฐฐ์ด๋ก ์ ๋ฌํ๋ค
โ๏ธbind
ํจ์๋ฅผ ์ฆ์ ์คํํ์ง ์๊ณ ๋๊ฒจ๋ฐ์ ์ธ์๋ฅผ ๋ฐํ์ผ๋ก ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์ด๋ค
๋ฐ์ธ๋ฉํ ํจ์๋ ์๋ณธ ํจ์ ๊ฐ์ฒด๋ฅผ ๊ฐ์ธ๋ ํจ์๋ก์ call, apply์ ๊ฐ์ด ํจ์๊ฐ ๊ฐ๋ฆฌํค๊ณ ์๋ this๋ฅผ ๋ณ๊ฒฝํ๊ฒ ๋์ง๋ง ํด๋น this๊ฐ ํธ์ถ๋์ง๋ ์๋๋ค=> ๋ณ์๋ฅผ ํ ๋นํ์ฌ ํธ์ถํ๋ ํํ๋ก ์ฌ์ฉ
func.bind(thisArg[, arg1[, arg2[, ...]]])
let person1 = {
name: 'Koding'
};
let person2 = {
name: 'Kim',
study: function() {
console.log(this.name + '์ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.');
}
};
person2.study();//Kim์ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
// bind()
let student = person2.study.bind(person1);
student(); //Koding์ ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค.
person2 ๊ฐ์ฒด์ study() ๋ฉ์๋๋ฅผ person1 ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉํ๊ณ , student() ํจ์๋ฅผ ํธ์ถํ๋ค
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋งคํฌ๋กํ์คํฌ vs ๋ง์ดํฌ๋กํ์คํฌ (6) | 2023.10.21 |
---|---|
FileReader VS URL.createObjectURL (0) | 2023.07.13 |
์์ฑ์ ํจ์์ ์ํ ๊ฐ์ฒด ์์ฑ (6) | 2023.03.09 |
๋๋๊ทธ์ค ๋๋กญ(js) (7) | 2023.03.07 |
๊ฐ๋จํ js ๋ฌดํ์คํฌ๋กค๊ณผ ์ด๋ฒคํธ์์ (6) | 2023.03.03 |