
this๋ ๋ค๋ฅธ ๊ฐ์ฒด์งํฅ ์ธ์ด์๋ ๋ค๋ฅด๊ฒ ์๋ฐ์คํฌ๋ฆฝํธ ์์๋ ์ด๋์์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.this๊ฐ ์ํฉ๋ณ๋ก ๋ง์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ ์ ์๊ณ ์ฌ์ฉํ๋ฉด ์ ์ฉํ ์๋ ์๋ค.
this๋ ์คํ์ปจํ
์คํธ๊ฐ ์์ฑ๋ ๋ ๊ฒฐ์ ๋๋ค. ์ด๋ง์ this๋ฅผ bindํ๋ค๊ณ ํด์ ThisBinding์ด๋ผ๊ณ ํ๋๊ฒ์ด๋ค.
๊ธฐ๋ณธ ๋ฐ์ธ๋ฉ์ ์ ์ญ๊ณต๊ฐ์์์ this๋ฅผ ๋งํ๋ค.
์ ์ญ๊ณต๊ฐ์์ this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๋ฐํ์์ ๋ฐ๋ผ this๋ window(๋ธ๋ผ์ฐ์ ํ๊ฒฝ) ๋๋ global(node ํ๊ฒฝ)์ ๊ฐ๊ฐ ๊ฐ๋ฆฌํจ๋ค.

์ด๋ ์กฐ์ฌํด์ผ ๋ ๊ฒ์ ์๊ฒฉ๋ชจ๋(use strict)๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ ์ญ๊ฐ์ฒด๊ฐ ๊ธฐ๋ณธ ๋ฐ์ธ๋ฉ ๋์์์ ์ ์ธ๋๋ค. ์ด ๊ฒฝ์ฐ์๋ this๊ฐ ๋ฐ์ธ๋ฉ ๋ ๊ฐ์ฒด๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ undefined ๊ฐ์ ๊ฐ์ง๊ฒ ๋๋ค.
๋ฐํ์ ํ๊ฒฝ์ด๋ javascript๋ก ๋ง๋ค์ด๋ ํ๋ก๊ทธ๋จ์ด ๊ตฌ๋์ค์ธ ํ๊ฒฝ์ ๋งํ๋ค. htmlํ์ผ ์์ ์จ๊ฒจ๋์ด ํฌ๋กฌ๋ธ๋ผ์ฐ์ ๋ฑ์์ ์ฐ๋ค๊ณ ํ๋ค๋ฉด ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์ธ ๊ฒ์ด๋ค.console.log(this); // ๋ฐํ์ ํ๊ฒฝ์ ๋ฐ๋ผ Window ๋ browser๊ฐ์ฒด๊ฐ ๋์จ๋ค.
๐ก node ํ๊ฒฝ์์ ์ ์ญ์ฝ๋์ this๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด ๋น ๊ฐ์ฒด๊ฐ ๋์ค๊ฒ ๋๋ค. ์ด ๋น ๊ฐ์ฒด๋ ์ฌ์ค ๋ชจ๋ ๊ฐ์ฒด์ ์๋ exports ๊ฐ์ฒด์ ์๋ฒฝํ๊ฒ ๋์ผํ ๊ฐ์ฒด์ด๋ค.
Node JS ํ๊ฒฝ์์ this๊ฐ ๋ ๊ฐ์ง ๋ฐฉํฅ์ผ๋ก ๋ฐ์ธ๋ฉ ๋๋ค.
ํจ์ ๋ด์์์ this
function sayHello() {
console.log('Hello, ' + this.name);
// "this"๋ ์ ์ญ ๊ฐ์ฒด(window)๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
// ํธ์ถ ์ฃผ์ฒด๋ฅผ ๋ช
์ํ ์ ์๊ธฐ ๋๋ฌธ์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค.
}
sayHello();
ํจ์์์ this๋ฅผ ์ฐ์ด๋ณด๋ฉด ํธ์ถ ์ฃผ์ฒด๋ฅผ ๋ช ์ํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ญ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๋ค.
js์์ ํจ์๋ ๋จ๋
์ผ๋ก ํธ์ถ๋ ์ ์์ ๋ฟ๋ง ์๋๋ผ ๊ฐ์ฒด์ ๋ฉ์๋๋ก๋ ํธ์ถ์ด ๋๋ค. ๋ฉ์๋๋ก ํธ์ถ์ด ๋ ๊ฒฝ์ฐ this๊ฐ ์ ๋ฐ๋ก ์์ ์๋ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ ์์์ ๋ฐ์ธ๋ฉ ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค.
๐ก ํจ์์ ๋ฉ์๋๋ ์๋ก ๋น์ทํด ๋ณด์ด์ง๋ง ๋ ๋ฆฝ์ฑ์ ๊ธฐ์ค์ผ๋ก ์ฐจ์ด๊ฐ ์๋ค. ํจ์๋ ๊ทธ์์ฒด๋ก ๋ ๋ฆฝ์ ์ธ ๊ธฐ๋ฅ์ ์ํํ๋ค. ํ์ง๋ง ๋ฉ์๋๋ ์์ ์ ํธ์ถํ ๋์ ๊ฐ์ฒด์ ๋ํ ๋์์ ์ํ ํ๋๊ฒ์ด๋ค.
ํธ์ถ ๋ฐฉ์์ ์ฐจ์ด :ํจ์()?๊ฐ์ฒด.๋ฉ์๋()
๊ฐ์ฒด ๋ฉ์๋๋ด์์์ this
const person = {
name: 'John',
greet: function() {
console.log('Hello, ' + this.name);
// "this"๋ ํ์ฌ ๊ฐ์ฒด(person)๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
// ํธ์ถ ์ฃผ์ฒด๋ฅผ ๋ช
์ํ ์ ์๊ธฐ ๋๋ฌธ์ this๋ ํด๋น ๊ฐ์ฒด(person)๋ฅผ ์๋ฏธํ๋ค.
}
};
person.greet(); // ์ถ๋ ฅ: "Hello, John"
ํจ์์์ this๋ฅผ ์ฐ์ด๋ณด๋ฉด ํธ์ถ ์ฃผ์ฒด๋ฅผ ๋ช ์ํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ญ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๊ณ ๊ฐ์ฒด ๋ฉ์๋ ๋ด์์๋ ํธ์ถ์ฃผ์ฒด(person{})๋ฅผ ๋ช ์ํ์ฌ ๊ฐ์ฒด๋ด์์ ์ถ๋ ฅ์ด ๋๋ค.
let obj1 = {
outer: function () {
console.log("outer", this);
let innerFunc = function () {
console.log("inner", this);
};
innerFunc(); //inner ์ ์ญ๋ณ์
let obj2 = {
innerMethod: innerFunc,
};
obj2.innerMethod(); // inner obj2
},
};
obj1.outer(); // outer obj1
this ๋ฐ์ธ๋ฉ์ ๊ดํด์๋ ํจ์๋ฅผ ์คํํ๋
๋น์์ ์ฃผ๋ณ ํ๊ฒฝ( ๋ฉ์๋ ๋ด๋ถ์ธ์ง, ํจ์ ๋ด๋ถ์ธ์ง )์ ์ค์ํ์ง ์๊ณ, ์ค์ง ํด๋น ํจ์๋ฅผ ํธ์ถํ๋ ๊ตฌ๋ฌธ ์์ ์ ๋๋ ๋๊ดํธ ํ๊ธฐ๊ฐ ์๋์ง๊ฐ ๊ด๊ฑด์ด๋ผ๋ ๊ฒ์ ์ ์ ์๋ค.
์ฝ๋ฐฑ ํจ์๋ ์ ๋ฌ์ธ์๋ก ๋ค๋ฅธ ํจ์์ ์ ๋ฌ๋๋ ํจ์๋ฅผ ๋งํ๋ค.
์ฝ๋ฐฑํจ์ ๋ด๋ถ์ this๋ ํด๋น ์ฝ๋ฐฑํจ์๋ฅผ ๋๊ฒจ๋ฐ์ ํจ์(๋ฉ์๋)๊ฐ ์ ํ ๊ท์น์ ๋ฐ๋ผ ๊ฐ์ด ๊ฒฐ์ ๋๋ค. ์ฝ๋ฐฑ ํจ์๋ ํจ์๊ธฐ ๋๋ฌธ์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ์ง๋ง, ์ฝ๋ฐฑํจ์๋ฅผ ๋๊ฒจ๋ฐ์ ํจ์์์ ์ฝ๋ฐฑ ํจ์์ ๋ณ๋๋ก this๋ฅผ ์ง์ ํ ๊ฒฝ์ฐ๋ ์์ธ์ ์ผ๋ก ๊ทธ ๋์์ ์ฐธ์กฐํ ์ ์๋ค. (Ex. addEventListener() )
// ๋ณ๋ ์ง์ ์์ : ์ ์ญ๊ฐ์ฒด
setTimeout(function () { console.log(this) }, 300);
// ๋ณ๋ ์ง์ ์์ : ์ ์ญ๊ฐ์ฒด
[1, 2, 3, 4, 5].forEach(function(x) {
console.log(this, x);
});
// addListener ์์์์ this๋ ํญ์ ํธ์ถํ ์ฃผ์ฒด์ element๋ฅผ returnํ๋๋ก ์ค๊ณ๋์์
// ๋ฐ๋ผ์ this๋ button์ ์๋ฏธํจ
document.body.innerHTML += '<button id="a">ํด๋ฆญ</button>';
document.body.querySelector('#a').addEventListener('click', function(e) {
console.log(this, e);
});
- โ setTimeout ํจ์, forEach ๋ฉ์๋๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ ๋
๋์์ด ๋ this๋ฅผ ์ง์ ํ์ง ์์ผ๋ฏ๋ก`, this๋ ๊ณง window๊ฐ์ฒด- โ addEventListner ๋ฉ์๋๋ ์ฝ๋ฐฑ ํจ์ ํธ์ถ ์,
์์ ์ this๋ฅผ ์์ํ๋ฏ๋ก, this๋ addEventListner์ ์๋ถ๋ถ(button ํ๊ทธ)
๋ง์ผ ์ฐ๋ฆฌ๊ฐ ์ง์ ์๋โฌ๏ธ์ ๊ฐ์ ์ฝ๋ฐฑํจ์๋ฅผ ๋ง๋ค์๋ค๋ฉด showReturnValue()ํจ์์์ obj.name์ด ์ถ๋ ฅ๋์ผ ํ ๊ฒ ๊ฐ๋ค. ํ์ง๋ง undefined๊ฐ ์ถ๋ ฅ๋๋ค.
const obj = {
name: "hoheesu",
getName() {
return this.name;
},
};
function showReturnValue(callBack) {
console.log(callBack());
}
console.log(obj.getName()); //hoheesu
showReturnValue(obj.getName); //undefined

๋ถ๋ช
obj.getName๊ณผ callBack์ ์๋ฒฝํ๊ฒ ๋์ผํ ํจ์์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ณ ์๋๋ฐ ์๋ก์ ๊ฐ์ ๋ค๋ฅด๊ฒ ๋์ค๋ ๊ฒ์ด๋ค. ๊ทธ ์ด์ ๋ ์ ์ฐ์ฐ์ ์๋ค.

์ฐ๋ฆฌ๊ฐ ์ ์ฐ์ฐ์ด๋ ๋๊ดํธ์ฐ์ฐ์ ํตํด ๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ ๊ทผ์ ํ๋ฉด ์ฐธ์กฐํ์
์ด๋ผ๋ ํน๋ณํ ๊ฐ์ ๋ฐํํด์ค๋ค. < ์ฐธ์กฐํ์
์ JS๋ช
์ธ์์์๋ง ์ฌ์ฉ๋๋ ํ์
์ด๋ค >
์ฐธ์กฐํ์ : (base, name, strict)๋ฅผ ํจ๊ป ๋๊ฒจ์ฃผ๋ ํ์ ์ด๋ค.
obj.getName()์ฒ๋ผ ๋ฐ๋ก ํธ์ถ์ ํ๊ฒ ๋๋ฉด์ฐธ์กฐ ํ์ ์ ํตํด ์์์ ๋ฐ์ธ๋ฉ์ ํ์ง๋งfunction showReturnValue(callBack) { callBack() }์ฒ๋ผ ๋ณต์ ํ์ฌ ๋ค๋ฅธ๋ณ์์ ๊ฐ์ ๋ด๋๋ค๋ฉด ํจ์์์ฐธ์กฐ๊ฐ๋ง ๋จ๊ฒ๋๋ค.
โ ์ ์ฐ์ฐ์ ํตํด ์ป์ ๊ฐ์๋ฐ๋ก ํธ์ถํ์ง ์๋๋ค๋ฉด์์์ ๋ฐ์ธ๋ฉ์ ๊ธฐ๋ํ ์ ์๋ค.
์ด๋ ๋ฏ ๋ณต์ ๋ฅผ ๊ฑฐ์น๋ค๋ฉด ๋ฐ์ธ๋ฉ๋ this๋ฅผ ์ฝ๊ฒ ์์ด๋ฒ๋ฆฌ๋ ๋ฌธ์ ๋๋ฌธ์๋ช ์์ ์ผ๋ก ThisBinding์ ํ ์์๋ ๋ฐฉ๋ฒ๋ค์ด ์๋ค
ํด๊ฒฐ ๋ฐฉ๋ฒ.
ํจ์๊ฐ์ฒด๋ call() / apply() / bind() ๋ฑ์ ๋ฉ์๋๋ฅผ ํตํด์ ์๋์ผ๋ก ๋ถ์ฌ๋๋ ์ํฉ๋ณ this์ ๊ท์น์ ๊นจ๊ณ this์ ๋ณ๋์ ๊ฐ์ ๋ช
์์ ์ผ๋ก ๋ฐ์ธ๋ฉํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
call()๊ณผ apply()๋ ์ฒซ๋ฒ์งธ ์ธ์๋ก this๋ฅผ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ค. ๋ ๋ฉ์๋๋ ๊ฑฐ์ ๋์ผํ์ง๋ง, ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ก ๊ฐ์ฒด์ ์ธ์๋ฅผ ์ ๋ฌํด์ฃผ๋๋ฐ(e.g. ์์ฑ์์ ๋งค๊ฐ๋ณ์), call์ ๋งค๊ฐ๋ณ์์ ๋ชฉ๋ก, apply๋ ๋ฐฐ์ด์ ๋ฐ๋๋ค๋ ์ฐจ์ด์ ์ด ์๋ค.

์ฌ์ฉ๋ฐฉ๋ฒ ์์)
// ์ ์ญ๊ฐ์ฒด๋ฅผ ๋ฐ๋ผ๋ณด๋ ํ์์์ ๋ช
์์ binding
let func = function (a, b, c) {
console.log(this, a, b, c);
};
func.call({ x: 1 }, 4, 5, 6); // { x: 1 } 4 5 6
func.apply({ x: 1 }, [4, 5, 6]); // { x: 1 } 4 5 6
// ํธ์ถ์ฃผ์ฒด๊ฐ ์๋ ๊ฒฝ์ฐ ๋ช
์์ binding
let obj = {
a: 1,
method: function (x, y) {
console.log(this.a, x, y);
},
};
obj.method.call({ a: 1 }, 5, 6); // 4 5 6
obj.method.apply({ a: 1 }, [5, 6]); // 4 5 6
call()๊ณผ apply()๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์ ๋ด๋ถ์์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถ(๊ณตํต๋ ๋ด์ฉ์ ๋ฐ๋ณต ์ ๊ฑฐ)ํ ์ ์๋ค.
function Person(name, gender) {
this.name = name;
this.gender = gender;
}
function Student(name, gender, school) {
Person.call(this, name, gender); // ์ฌ๊ธฐ์ this๋ student ์ธ์คํด์ค!
this.school = school;
}
function Employee(name, gender, company) {
Person.apply(this, [name, gender]); // ์ฌ๊ธฐ์ this๋ employee ์ธ์คํด์ค!
this.company = company;
}
let kd = new Student('๊ธธ๋', 'male', '์์ธ๋');
let ks = new Employee('๊ธธ์', 'female', '์ผ์ฑ');
โฌ๏ธ์ ์ฝ๋์์ Student, Employee๋ ๋ชจ๋ name๊ณผ gender ๊ฐ ํ์ํPerson์ด๋ค. ๊ทธ๋ฌ๋ Student์ Employee ์ธ์คํด์ค๋ฅผ ๋ง๋ค ๋ ๋ง๋ค ์ธ ๊ฐ์ง ์์ฑ์ ๋ชจ๋ ๊ฐ ์์ฑ์ ํจ์์ ๋ฃ๊ธฐ ๋ณด๋ค๋ Person์ด๋ผ๋ ์์ฑ์ ํจ์๋ฅผ ๋ณ๋๋ก ๋นผ๋๊ฒ ๊ตฌ์กฐํ์ ๋์์ด ๋ ๋๋ค.
call๊ณผ๋ ๋ค๋ฅด๊ฒ ์ฆ์ ํธ์ถํ์ง๋ ์๊ณ ๋๊ฒจ๋ฐ์ this ๋ฐ ์ธ์๋ค์ ๋ฐํ์ผ๋ก ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์ด๋ค.
โ ํจ์์ this๋ฅผ ๋ฏธ๋ฆฌ ์ ์ฉํ๋ค
โ ๋ถ๋ถ ์ ์ฉ ํจ์ ๊ตฌํํ ๋ ์ฉ์ดํ๋ค.
// ํจ์์ this ๋ฏธ๋ฆฌ ์ ์ฉ
let func = function (a, b, c, d) {
console.log(this, a, b, c, d);
};
let bindFunc1 = func.bind({ x: 1 }); // ๋ฐ๋ก ํธ์ถ๋์ง๋ ์๋๋ค.
bindFunc1(5, 6, 7, 8); // { x: 1 } 5 6 7 8
// ๋ถ๋ถ ์ ์ฉ ํจ์ ๊ตฌํ
let bindFunc2 = func.bind({ x: 1 }, 4, 5); // 4์ 5๋ฅผ ๋ฏธ๋ฆฌ ์ ์ฉ
bindFunc2(6, 7); // { x: 1 } 4 5 6 7
bindFunc2(8, 9); // { x: 1 } 4 5 8 9
console.log(func.name); // func
console.log(bindFunc1.name); // bound func
console.log(bindFunc1.name); // bound func
bind ๋ฉ์๋๋ฅผ ์ ์ฉํด์ ์๋ก ๋ง๋ ํจ์๋ name ํ๋กํผํฐ์ โbound funcโ ๋ผ๋ ์ ๋์ด์ธ nameํ๋กํผํฐ๊ฐ ๋ถ๋๋ค. โก๏ธ ์ดํ์ ํจ์๋ฅผ ์ถ์ ํ๊ธฐ ์ฝ๋ค.
let func = function (a, b, c, d) {
console.log(this, a, b, c, d);
};
let bindFunc = func.bind({ x:1 }, 4, 5);
// func์ bindFunc์ name ํ๋กํผํฐ์ ์ฐจ์ด๋ฅผ ์ดํด๋ณด์ธ์!
console.log(func.name); // func
console.log(bindFunc.name); // bound func
๊ทธ๋ ๋ค๋ฉด โฌ๏ธ์์ ์๋ ์ฝ๋ฐฑํจ์์์์ ๋ฐ์ธ๋ฉ ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐ ํ ์ ์๋์ง ์ฝ๋๋ก ์์ฑ์ ํด๋ณด์๋ฉด,
const obj = {
name: "hoheesu",
getName() {
return this.name;
},
};
function showReturnValue(callBack) {
console.log(callBack.call(obj)); // console.log(callBack.apply(obj));
}
showReturnValue(obj.getName); //undefined
์ด๋ฐ์์ผ๋ก call๊ณผ apply๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ธ๋ฉ์ ํด์ค๋๋ ํจ์๋ฅผ ํธ์ถํ ๋ ๋ฐ์ธ๋ฉ์ ํด์ค ์ ์๋ค.
const obj = {
name: "hoheesu",
getName() {
return this.name;
},
};
function showReturnValue(callBack) {
console.log(callBack());
}
const boundGetName = obj.getName.bind(obj);
showReturnValue(boundGetName); //undefined
bind๋ฅผ ์ด์ฉํ์ฌ ๋ช ์์ this๋ฐ์ธ๋ฉ์ ํ ๋๋ ๋ฐํ์ผ๋ก ์๋ก์ด ํธ์ถํจ์๋ฅผ ๋ง๋ค์ด this ๋ฐ์ธ๋ฉ์ ํ๋ ๋ฐฉ์์ด๋ค.
JavaScript์์ ํจ์๋ฅผ new ์ฐ์ฐ์๋ก ํธ์ถํ๊ฒ ๋๋ฉด ์์ฑ์ ํจ์๋ก์์ ์ญํ ์ ํจ์๊ฐ ์ํํ ์ ์๊ฒ ๋๋ค.
์์ฃผ ๊ฐ๋จํ๊ฒ ์ค๋ช
ํ์๋ฉด new์ฐ์ฐ์๋ก ํธ์ถํ๊ฒ๋๋ฉด โก๏ธ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑ โก๏ธ ํจ์์ ์ฝ๋๋ฅผ ์คํ โก๏ธ ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๋ฐํ ์ด ๊ณผ์ ์์ this๋ ์๋ก์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ binding๋๋ค.
์ฝ๋๋ก ํํ์ ํ๋ค๋ฉด โฌ๏ธ์๋์ฌ์ง์ฒ๋ผ ์์ฑ ํ ์ ์์๊ฒ์ด๋ค.
โ ์์ฑ์ ํจ์ ๋ด๋ถ์์์ this๋ ํญ์ ์ธ์คํด์ค๋ฅผ ์ง์นญํ๋ค.
const Cat = function (name, age) {
this.bark = "์ผ์น";
this.name = name;
this.age = age;
};
const choco = new Cat("์ด์ฝ", 7); //this : choco
const nabi = new Cat("๋๋น", 5); //this : nabi
โฌ๏ธ ์ ์ฝ๋์์ ์์ฑ์ ํจ์๋ function (name, age){ ... } ๋ถ๋ถ์ด๋ค. ์์ฑ์ ํจ์ ๋ด๋ถ์ this๋ ์๋กญ๊ฒ ์์ฑ๋ ์ธ์คํด์ค(์ ์ฝ๋์์๋ choco์ nabi )๋ฅผ ์ง์นญํ๋ค.
๋ง์ผ ํจ์๊ฐ ์ค๋ณตํด์ ๋ฐ์ธ๋ฉ์ ํ๊ฒ ๋๋ค๋ฉด
New ๋ฐ์ธ๋ฉ > ๋ช ์์ ๋ฐ์ธ๋ฉ > ์์์ ๋ฐ์ธ๋ฉ > ๊ธฐ๋ณธ ๋ฐ์ธ๋ฉ
์ฐ์ ์์์ ๋ฐ๋ผ ๋ฐ์ธ๋ฉ๋๋ค.
ES6์์ ์ฒ์ ๋์
๋ ํ์ดํ ํจ์๋, ์คํ ์ปจํ
์คํธ๋ฅผ ์์ฑํ ๋ this ๋ฐ์ธ๋ฉ ๊ณผ์ ์์ฒด๊ฐ ์๋ค. this๋ฐ์ธ๋ฉ ์์ฒด๊ฐ ์๋ค๋ ๋ง์ : <this ์ด์ ์ ๊ฐ-์์๊ฐ-์ด ์ ์ง๋๋ค>๋ ๊ฒ์ด๋ค.
ES6์์๋ ํจ์ ๋ด๋ถ์์ this๊ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ๋ฐ๋ผ๋ณด๋ ๋ฌธ์ ๋๋ฌธ์ ํ์ดํํจ์๋ฅผ ๋์
ํ ๊ฒ์ด๋ค.
์ผ๋ฐ ํจ์์ ํ์ดํ ํจ์์ ๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์ this binding ์ฌ๋ถ์ด๋ค.
let obj = {
outer: function () {
let innerFunc1 = () => {
console.log("innerFnc1", this);
};
innerFunc1(); // innerFnc1 { outer: [Function: outer] }
function innerFunc2() {
console.log("innerFnc2", this);
}
innerFunc2(); //innerFnc2 global{}
},
};
obj.outer();
โฌ๏ธ ์ฝ๋๋ฅผ ๋ณด๋ฉด ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ this๋ฅผ ํธ์ถํ๋ฉด ์์ ์ ์์๊ฐ์ธ outer๊ฐ ๋ฐ๋ก ์ถ๋ ฅ๋์ง๋ง ๊ธฐ๋ณธ ํจ์ ์ ์ธ์์ผ๋ก this๋ฅผ ํธ์ถํ๋ฉด ์ ์ญ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๋ค.
๋ด๋ถ ์ค์ฝํ์ ์ด๋ฏธ ์กด์ฌํ๋ this๋ฅผ ๋ณ๋์ ๋ณ์์ ํ ๋นํ๋ ๋ฐฉ์
let obj1 = {
outer: function () {
console.log("outer", this); // outer { outer: [Function: outer] }
let self = this;
console.log("self", this); // self { outer: [Function: outer] }
let innerFnc = function () {
console.log("inner", self); // inner { outer: [Function: outer] }
};
innerFnc();
},
};
obj1.outer();
โฌ๏ธ ์ ์ฝ๋์ฒ๋ผ self๋ผ๋ ๋ณ์์ this๋ฅผ ํ ๋นํจ์ผ๋ก์จ ํจ์๋ฅผ ํธ์ถํ ๋ ๋ ์์ปฌ์ค์ฝํ์ ์ํด self์ ์ ๊ทผํ ์ ์๊ฒ๋๊ณ , self๊ฐ ํฌํจ๋์ด์๋ ์์ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ ๊ฒ์ด๋ค.
const outer = {
name: "hoheesu",
innerFnc: function () {
setTimeout(function () {
console.log(this.name);
}, 1);
},
};
outer.innerFnc(); //undefined
โฌ๏ธ ์ ์ฝ๋๋ undefined๊ฐ ์ถ๋ ฅ๋๋ ์ฝ๋์ด๋ค. setTimeout์ ์ฝ๋ฐฑ์ผ๋ก ๋๊ฒจ์ง ํจ์์ this๋ setTimeout์ ์ํด ์คํ๋ ๋ ์ ์ญ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ์ด ๋๊ธฐ ๋๋ฌธ์ด๋ค. ์ฝ๋ฐฑ ๋ด๋ถ์์ ํด๋น ํจ์๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ์ ๊ทผํ๊ธฐ ์ํ ๋ฐฉ์์ ์ฌ๋ฌ๊ฐ๊ฐ ์๋ค.
const obj = {
name: "hoheesu",
logThisLater: function () {
setTimeout(() => {
console.log(this.name);
}, 100);
},
};
obj.logThisLater();
const obj = {
name: "hoheesu",
logThisLater: function () {
const _this = this;
setTimeout(function () {
console.log(_this.name);
}, 100);
},
};
obj.logThisLater();
์ฐธ๊ณ ๋ฌธํ
[10๋ถ ํ
์ฝํก] ๐ฅฆ ๋ธ์ฝ์ This