this
์ ์
์๋ฐ์คํฌ๋ฆฝํธ์ this๋ ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์์ด๋ค. ๋ผ๊ณ ์ฐ์ฌ์๋ค. ๋ง์ด ์ข ์ด๋ ค์ด๋ฐ ๊ฐ๋จํ ์์๋ฅผ ํตํด ์์๋ณด์.
let person = {
firstName: "Kim",
lastName: "Chungyeon",
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.fullName()); //Kim ChungYeon
์ด ์์ ์ฝ๋๋ this ์ ์ ์ ์ค "์์ ์ด ์ํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋~"
์ ๋ํ ์ฝ๋์ด๋ค. ์์์์ this๋person ๊ฐ์ฒด์ fullName ๋ฉ์๋์์ ์ฌ์ฉ๋์๋ค. this๋ ์คํ ์ค์ธ ํจ์๊ฐ ์ํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ฏ๋ก ์ฌ๊ธฐ์ this๋ ๋ฐ๋ก person ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
function Person(name) {
this.name = name;
}
Person.prototype.introduce = function() {
console.log("์๋
ํ์ธ์, ์ ์ด๋ฆ์ " + this.name + "์
๋๋ค.");
}
// ์ธ์คํด์ค ์์ฑ
let person1 = new Person("Kim");
let person2 = new Person("Lee");
person1.introduce(); // ์๋
ํ์ธ์, ์ ์ด๋ฆ์ Kim์
๋๋ค.
person2.introduce(); //
์ด๋ฒ์๋ "์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋~"
์ ๋ํ ์์ ์ฝ๋๋ฅผ ๋ณด์.
์์์์ this๋ Person ๊ฐ์ฒด์ ํ๋กํ ํ์
์ ์ถ๊ฐ๋ ๋ฉ์๋์ธ introduce์์ ์ฌ์ฉ๋์๋ค. ์ฆ, ์ฌ๊ธฐ์ ๋งํ๋ "์์ "์ ํ๋กํ ํ์
์ ๋ง๋ Person ๊ฐ์ฒด์ด๋ฉฐ, Person ๊ฐ์ฒด๊ฐ ์์ฑํ ์ธ์คํด์ค๊ฐ ๋ฐ๋ก person1๊ณผ person2 ์ด๋ค. ๋ฐ๋ผ์ this๋ Person์ด ์์ฑํ ์ธ์คํด์ค์ธ person1๊ณผ person2๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ
์ด๋ค.
๋ ๋ฒ์งธ ์์ ์ฝ๋์์ ์์๋ณธ ๊ฒ์ฒ๋ผ, this๋ ํธ์ถํ๋ ๋์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฐ๋ฆฌํค๋ ๊ฐ์ด ๊ฒฐ์ ๋๋ฉฐ, this์ ๊ฐ์ด ๊ฒฐ์ ๋๋ ๊ฒ์ ๋ฐ์ธ๋ฉ์ด๋ผ๊ณ ํ๋ค. ๋ฐ์ธ๋ฉ์ ๋ํด ์กฐ๊ธ ๋ ์ดํด๋ณด์
๋ฐ์ธ๋ฉ
์ผ๋ฐ ํจ์์์ this๋ ์ ์ญ ๊ฐ์ฒด์ธ window์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐ์ธ๋ฉ ๋๋ค. (node.js์์๋ global)
console.log(this) // window
function test() {
console.log(this); // Window {0: Window, ...}
function inner() {
console.log(this); // Window {0: Window, ...}
}
inner();
}
test();
๋ฉ์๋ ํธ์ถ ์์๋ ์ฐ๋ฆฌ๊ฐ ์์ ์ฝ๋ 1 ์์ ํ์ธ ํ๋ ๊ฒ์ฒ๋ผ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ค.
์ ์ํ ์ ์, ๋ง์ฝ ํ์ดํ ํจ์๋ก ์์ฑ์ด ๋ ๊ฒฝ์ฐ, this ๋ ์์ ์ปจํ ์คํธ์ this๋ฅผ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ ์๋ ์์์์๋ ์ ์ญ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ค.
์๋์์ ๋ค์ ํ ๋ฒ ์ดํด๋ณด์.
const obj = {
normal: function() {
console.log('normal fn this:', this);
},
arrow: () => {
console.log('arrow fn this:', this);
},
}
obj.normal(); // normal fn this: obj
obj.arrow(); // arrow fn this: window
๋ค์์ผ๋ก ์์ฑ์ ํจ์์์๋ ์์ฑ์ ํจ์๋ก ๋ถํฐ ๋ง๋ค์ด์ง๋ ์ธ์คํด์ค์ ๋ฐ์ธ๋ฉ ๋๋ค.
function User(name) {
this.name = name;
}
const user1 = new User('Kim');
console.log(user1.name); // Kim
๊ทธ๋ฐ๋ฐ ๋ง์ฝ new
ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์ ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋๋ ๊ฒฝ์ฐ, ์ ์ญ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉ ๋๋ค.
์์์ ์ ์ ์ดํด๋ณด์๋๋ฐ, ํ์ดํํจ์์์ ์ฌ์ฉ๋ this์ ๊ฒฝ์ฐ, Lexical this๋ฅผ ๋ฐ์ธ๋ฉ
ํ๋ค. ์ฝ๊ฒ ๋งํด, ํ์ดํ ํจ์์ ๋ ์์ปฌ ์ค์ฝํ์ ์กด์ฌํ๋ this๋ฅผ ๋ฐ์ธ๋ฉ
ํ๊ฒ ๋๋ค.
const arrow= () => {
console.log(this);
}
arrow(); // window
// arrow ๋ฉ์๋์ ์ค์ฝํ์ ์ ์๋์์ผ๋ฏ๋ก, this๊ฐ arrow ๋ฉ์๋์ this๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
const obj = {
arrow: function() {
const arrowInner = () => {
console.log(this);
}
arrowInner ();
}
};
obj.arrow(); // obj
์์ ์ฝ๋๋ฅผ ๋ณด๋ฉด obj ๊ฐ์ฒด์ arrow ๋ฉ์๋์ ๋ ์์ปฌ ์ค์ฝํ๋ ์์ฑ๋์์ ๋น์์ ํ๊ฒฝ์ธ obj ๊ฐ์ฒด์ด๊ณ , arrowInner ๋ ํ์ดํ ํจ์์ด๋ฏ๋ก, this๋ arrow ๋ฉ์๋์ ์ค์ฝํ์ this๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค. ๊ทธ๋ฐ๋ฐ arrow ๋ฉ์๋์ ์ค์ฝํ์ this๊ฐ ๋ฐ๋ก obj ๊ฐ์ฒด์ด๋ฏ๋ก this๋ obj ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ์ด๋ค.
๋ง์ง๋ง์ผ๋ก ์ด๋ฒคํธ ์ฒ๋ฆฌ์์ this๋ฅผ ์ดํด๋ณด์.
const button = document.getElementById('button');
const consoleThis = function (e) {
console.log(e.target === this, this);
};
button.addEventListener('click', consoleThis);
// <button id="button">click</button>
์ด๋ฒคํธ ์ฒ๋ฆฌ์์ this๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์ HTML ์์๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ์์ ์ฝ๋์์๋ button ์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์คฌ๊ธฐ ๋๋ฌธ์, console.log ์ ์ถ๋ ฅ ๋๋ ๊ฐ, this๋ ๋ฐ๋ก button ์์ ์์ฒด์ด๋ค.
Last updated