[์๋ฐ์คํฌ๋ฆฝํธ] this ์ ๋ฆฌ
์ฐธ๊ณ ์๋ฃ: ์ธํ๋ฐ ์ฝ์ด์๋ฐ์คํฌ๋ฆฝํธ(ํด๋์ค ํธ)
โญ๏ธ this
- ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์
- this๋ฅผ ํตํด ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค์ ํ๋กํผํฐ/๋ฉ์๋๋ฅผ ์ฐธ์กฐ
- ์๋ฌต์ ์ผ๋ก ์์ฑ
- ์ฝ๋ ์ด๋์์๋ ์ฐธ์กฐ ๊ฐ๋ฅ
- ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํ ์๊ธฐ ์ฐธ์กฐ ๋ณ์์ด๋ฏ๋ก ์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ฒด์ ๋ฉ์๋ ๋๋ ์์ฑ์ ํจ์ ๋ด๋ถ์์๋ง ์๋ฏธ๊ฐ ์์
- ํจ์๋ฅผ ํธ์ถํ๋ฉด ์ธ์์ this๊ฐ ์๋ฌต์ ์ผ๋ก ํจ์ ๋ด๋ถ์ ์ ๋ฌ
- ์ธ์๋ฅผ ์ง์ญ ๋ณ์์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ฒ๋ผ, this๋ ์ง์ญ๋ณ์์ฒ๋ผ ์ฌ์ฉ ๊ฐ๋ฅ
- this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ, ์ฆ, this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์์ ์ํด ๋์ ์ผ๋ก ๊ฒฐ์
- ํฌ๊ฒ ์ ์ญ์์ ์ฌ์ฉํ ๋์ ํจ์ ์์์ ์ฌ์ฉํ ๋๋ก ๋๋
โจ ์ฐธ๊ณ ๋ก, ๋ฐ์ธ๋ฉ์ด๋ โ
- ์๋ณ์์ ๊ฐ์ ์ฐ๊ฒฐํ๋ ๊ณผ์
- ๋ณ์ ์ ์ธ์ ๋ณ์ ์ด๋ฆ๊ณผ ํ๋ณด๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฃผ์๋ฅผ ๋ฐ์ธ๋ฉ
- this ๋ฐ์ธ๋ฉ์ this์ this๊ฐ ๊ฐ๋ฆฌํฌ ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉ
this๋ ์ ๋ฒ ๊ฐ์์ ์ด์ด ์ด๋ถ๋ถ์ ํด๋น !
โ CASE 1 : ์ ์ญ๊ณต๊ฐ์์ ํธ์ถ
๐ ์ ์ญ๊ณต๊ฐ์์ ํธ์ถ์ : window(์๋ฐ์คํฌ๋ฆฝํธ ๋ฐํ์) / global (Node.js)
- ๋ธ๋ผ์ฐ์ ๋ผ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฐํ์์ ๊ฒฝ์ฐ this๋ ํญ์ windows๋ผ๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ
- ์ ์ญ ๊ฐ์ฒด๋ โ ์ ์ญ ๋ฒ์์ ํญ์ ์กด์ฌํ๋ ๊ฐ์ฒด ( node.js์์ ์ ์ญ๊ฐ์ฒด๋ global)
a= 'a';
//"a"
this.a
//"a"
window.a
//"a"
// in browser console
this === window // true
// in Terminal
node
this === global // true
๐ป ์ฝ๋ )
function myFn(){
return this;
}
๐ ์ด ํจ์๋ฅผ ํธ์ถํ๋ ์๊ฐ์ ์ฃผ์ฒด๊ฐ ์ ์ญ๊ณต๊ฐ์ด๊ธฐ ๋๋ฌธ์, ํจ์ ์์์๋ ์ ์ญ๊ฐ์ฒด๊ฐ ๊ฐ๋ฅํ๋ค
โ CASE 2. ํจ์ ๋ด๋ถ
๐ this๋ฅผ ํจ์ ๋ด๋ถ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ : window/global ๊ฐ์ฒด ์ถ๋ ฅ
- ํจ์๋ ์ ์ญ์ ์ ์ธ๋ ์ผ๋ฐ ํจ์์ ๊ฐ์ฒด ์์ ๋ฉ์๋๋ก ํฌ๊ฒ ๊ตฌ๋ถ
- ๊ฐ์ฒด ์์ ์ ์ธ๋ ํจ์๋ฅผ ์ ์ญ์ ์ ์ธ๋ ํจ์์ ๊ตฌ๋ถํ๊ธฐ ์ํด ๋ฉ์๋๋ผ๊ณ ํจ
- ๊ทธ๋ฐ๋ฐ ์ ์ญ์ ์ ์ธ๋ ์ผ๋ฐ ํจ์๋ ๊ฒฐ๊ตญ window ์ ์ญ ๊ฐ์ฒด์ ๋ฉ์๋. ์ฆ, ๋ชจ๋ ํจ์๋ ๊ฐ์ฒด ๋ด๋ถ์ ์์
- ์ด๋ this๋ ํ์ฌ ํจ์๋ฅผ ์คํํ๊ณ ์๋ ๊ทธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ
- ์ ๋ฆฌํ๋ฉด, ํจ์ ๋ด๋ถ์์ this์ ๊ฐ์ ํจ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ ์ํด ๋ฐ๋
- ์๊ฒฉ๋ชจ๋ ์ฌ๋ถ์ ๋ฐ๋ผ ์ฐธ์กฐ ๊ฐ์ด ๋ฌ๋ผ์ง
- ์๊ฒฉ๋ชจ๋์์ ์ผ๋ฐ ํจ์ ๋ด๋ถ์ this๋ undefined๊ฐ ๋ฐ์ธ๋ฉ ๋จ
๐ป ์ฝ๋ 1 )
var d = {
e : function() {
function f() {
console.log(this);
}
f(); // ์ ์ญ๊ฐ์ฒด
}
}
๐ f ๋ฉ์๋ ์์ this๋ฅผ ์ถ๋ ฅํ ๋, ์ ์ญ๊ฐ์ฒด๊ฐ ๋์ด.
๐ ์ด์ ๋ ๋ค์์ ๋งํ๊ฒ ์ง๋ง, f ๋ฉ์๋ ์์ ์๋ฌด๋ฐ ์ง์ ๊ฐ์ด ์์ด์ ๋ฐ๋ก ์ ์ญ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ
๐ป ์ฝ๋ 2)
function a() {
function b() {
console.log(this);
}
c(); // ์ ์ญ๊ฐ์ฒด ํธ์ถ
}
b(); // ์ ์ญ๊ฐ์ฒด ํธ์ถ
๐ ์๊ธฐํ ์๋ฐ์ ์๋ ๋ถ๋ถ.. c ํจ์๋ฅผ ํธ์ถํ์ ๋, cํจ์์ ์ฃผ์ฒด๋ b ํจ์์ด์ง๋ง ์ ์ญ๊ฐ์ฒด๋ฅผ ํธ์ถํ๋ค(this์ ๊ฐ์ ๋ฐ๋ก ์ ํด๋์ง ์์)
๐ b ํจ์๋ ๋ฉ์๋ ์์ this๋ฅผ ์ฐธ์กฐํ ํจ์๊ฐ ์์ด ์ ์ญ๊ฐ์ฒด๋ก ํธ์ถ
๐ป ์ฝ๋ 3 )
var value = 1;
var obj = {
value: 100,
foo: function() {
console.log("foo's this: ", this); // obj
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
}
bar();
}
};
obj.foo();
๐ foo ๋ฉ์๋ ํจ์์์๋ ๋ฐ๋ก ์ ์ฃผ์ฒด๊ฐ obj๋ผ๋ ํจ์. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ foo์ this๋ obj์ด๊ณ , this.value์ ๊ฐ์ obj์ value๊ฐ์ด 100์ด ์ถ๋ ฅ
๐ bar ๋ฉ์๋ ํจ์๋ ์ด์คํจ์๋ก์จ bar ํจ์ ์ ์ฃผ์ฒด๊ฐ fooํจ์. ํ์ง๋ง, foo ํจ์์์ ๋ช ํํ ์ด๋ค ์ฃผ์ฒดํ ๋งํ ๊ฐ์ด ์์ด์ this์ ๊ฐ๋ ์ ํด๋์ ๊ฒ์ด ์๊ธฐ ๋๋ฌธ์ ์ ์ญ๊ฐ์ฒด๊ฐ ์ถ๋ ฅ. this๋ window๋ผ๋ ์ ์ญ๊ฐ์ฒด, this.value๋ ์ ์ญ๋ณ์์ธ 1์ด ์ถ๋ ฅ
โ ๋ค์์ ์ ์ญ๊ฐ์ฒด์ ์ ์ญ๋ณ์๊ฐ ๋ค๋ฅด๊ฒ
๐ป ์ฝ๋ 4 )
var value = 1;
var obj = {
value: 100,
foo: function() {
setTimeout(function() {
console.log("callback's this: ", this); // window
console.log("callback's this.value: ", this.value); // 1
}, 100);
}
};
obj.foo();
๐ this๊ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์์ผ ๋์ง๋ง, ์ ์ญ๋ณ์ a๋ฅผ ํธ์ถ.
๐ ์ ์ญ๊ฐ์ฒด๋ ์ ์ญ๊ฐ์ฒด๊ณ , ์ ์ญ๋ณ์๋ ์ ์ญ๋ณ์๋ก ์๊ฐํ๋ฉด ๋ง์ง๋ง ๋ณ๊ฐ์ ๊ฐ๋ ์ผ๋ก ๋๋ฌด ์๊ฐํ์ง ๋ง์!
๐ ์์ธ์ ์ผ๋ก ) that์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ((์ฐธ๊ณ ๋ง))
var value = 1;
var obj = {
value: 100,
foo: function() {
var that = this; // Workaround : this === obj
console.log("foo's this: ", this); // obj
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
console.log("bar's that: ", that); // obj
console.log("bar's that.value: ", that.value); // 100
}
bar();
}
};
obj.foo();
โ CASE 3. ๋ฉ์๋
๐ ์ฐธ๊ณ , ๋ฉ์๋์์ this์ ์์ญ
๐ ๊ฐ์ฒด์ ๋ฉ์๋ ํจ์์์ this
๐ป ์ฝ๋ 1)
const fn = {
title: 'Hello World',
showTitle(){
console.log(this.title);
}
};
fn.showTitle();
//Hello World
๐ showTitle ๋ฉ์๋ ์์ fn์ด this๋ก ์ ํด์ ธ showTitle() ๋ฉ์๋๋ฅผ ํธ์ถํ์ ๋, ์ฝ์์ this๊ฐ fn์ผ๋ก ๊ฒฐ์ ๋๊ณ fn์ title๋ฅผ ์ฝ์์ ์ฐํ์ "Hello World"๊ฐ ์ถ๋ ฅ
๐ป ์ฝ๋ 2)
var a = {
b : function(){
console.log(this);
}
}
a.b(); //a๊ฐ this
๐ ๋ฉ์๋์์๋ ์ ์์ ์๋ a๊ฐ this๊ฐ ๋๋ ๊ฒ
๐ b ํจ์๋ฅผ a ๊ฐ์ฒด์ ๋ฉ์๋๋ก์ ํธ์ถ
โ ๋ฉ์๋๋ผ๋ ๊ฒ์ด ์๋๋ ํจ์์ธ๋ฐ, ์ด๋ค ๊ฐ์ฒด์ ๊ด๋ จ๋ ๋์์ ํ๊ฒ ๋๋ฉด ๊ทธ๋๋ ๋ฉ์๋๋ก์ ํธ์ถ
๐ป ์ฝ๋ 3)
var a = {
b : {
c: function(){
console.log(this);
}
}
}
a.b.c(); //a.b๊ฐ this
๐ ์ฌ๊ธฐ์๋ a.b๊น์ง๊ฐ this๋ฅผ ๊ฐ๋ฆฌํด
๐ป ์ฝ๋ 3-1)
const fn = {
title: "hello world",
tags : [1,2,3,4],
showTag(){
this.tags.forEach(function(tag){
console.log(tag);
console.log(this); //window
})
}
}
fn.showTag();
// 1
// window ๊ฐ์ฒด ์ถ๋ ฅ
// 2
// window ๊ฐ์ฒด ์ถ๋ ฅ
// 3
// window ๊ฐ์ฒด ์ถ๋ ฅ
// 4
// window ๊ฐ์ฒด ์ถ๋ ฅ
๐ ๊ณ ์ฐจ ํจ์์ ์ฝ๋ฐฑ ํจ์ ์์์ this๋ ์ฝ๋ฐฑํจ์๊ฐ ์ผ๋ฐ ํจ์์ด๊ธฐ ๋๋ฌธ์ ์ ์ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ
๐ showTag ํจ์ ์์ tag ํจ์๊ฐ ์กด์ฌํจ. ์ฌ๊ธฐ์ fn.showTag()์์ this๊ฐ fn์ด๋ผ ์๊ฐํ ์ ์์ง๋ง, ๊ณ ์ฐจ ํจ์์์ ์ฝ๋ฐฑํจ์๋ ๊ทธ์ ์ผ๋ฐํจ์์ผ ๋ฟ.
๐ป ์ฝ๋ 3-2) ํจ์์ ๋๋ฒ์งธ ์ธ์ ๋ฃ์ด์ this ์ธ์ ๊ฐ์ฒด ๋๊ฒจ์ฃผ๊ธฐ
const fn = {
title: "hello world",
tags : [1,2,3,4],
showTag(){
this.tags.forEach(function(tag){
console.log(tag);
console.log(this); //fn
}, this); //์ฌ๊ธฐ๋ ์ผ๋ฐ ํจ์ ๋ฐ๊นฅ, fn ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์์
}
}
fn.showTag();
// 1
// fn ๊ฐ์ฒด ์ถ๋ ฅ
// 2
// fn ๊ฐ์ฒด ์ถ๋ ฅ
// 3
// fn ๊ฐ์ฒด ์ถ๋ ฅ
// 4
// fn ๊ฐ์ฒด ์ถ๋ ฅ
๐ ์ฝ๋ 3-1์์ ํด๊ฒฐํ์ง ๋ชปํ ๊ณ ์ฐจ ํจ์ ์์์ ์ ์ญ๊ฐ์ฒด ์ฐธ์กฐ ์ ์ธํ ์ํ๋ ๊ฐ์ ์ฐธ์กฐํ๊ฒ๋ ๋ง๋ค์ด์ค ๋ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ฐ๋ก ์ฝ๋ฐฑํจ์ bind (๋ค์์ ์ค๋ช ์ ์ฝ์ผ๋ฉด ์ด ์ฝ๋ ๋ด์ฉ์ด ์ดํด๋ ๊ฒ์)
๐ ๋๋ฒ์งธ ์ธ์๋ก ๋ฐ๋ก this๋ฅผ ๋ฃ์์ผ๋ก์จ ๋ฐ๋ก ๋ฐ๊นฅ์ ์๋ fn์ด๋ผ๋ ๊ฐ์ฒด๋ฅผ this๋ก ๋๊ธธ ์ ์์
๐ป ์ฝ๋ 3-3) ํ์ดํ ํจ์ ์ฌ์ฉํ๊ธฐ
โจ ํ์ดํ ํจ์๋ โ
- function ํค์๋๋ก ์์ฑํ ์ผ๋ฐ ํจ์์ ํ์ดํ ํจ์์ ๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์ ๋ฐ๋ก this. ์ด๋ฅผ Lexical this๋ผ๊ณ ๋งํจ
- ํ์ดํ ํจ์ ์์์ this๋ ์ธ์ ๋ ์์ ์ค์ฝํ์ this๋ฅผ ๊ฐ๋ฆฌํด
- ์ผ๋ฐ ํจ์๋ ํจ์๋ฅผ ์ ์ธํ ๋ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๊ฐ ์ ์ ์ผ๋ก ๊ฒฐ์ ๋์ง ์๊ณ , ํจ์๋ฅผ ํธ์ถํ ๋ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋๋์ง์ ๋ฐ๋ผ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๋ฅผ ๋์ ์ผ๋ก ๊ฒฐ์ ํจ
- ํ์ดํ ํจ์๋ ํจ์๋ฅผ ์ ์ธํ ๋ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๊ฐ ์ ์ ์ผ๋ก ๊ฒฐ์
- ํ์ดํ ํจ์์ this ๋ฐ์ธ๋ฉ ๊ฐ์ฒด ๊ฒฐ์ ๋ฐฉ์์ ํจ์์ ์์ ์ค์ฝํ๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐฉ์์ธ ๋ ์์ปฌ ์ค์ฝํ์ ์ ์ฌ
- ํ์ดํ ํจ์๋ call, apply, bind ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ this๋ฅผ ๋ณ๊ฒฝํ ์ ์์
const fn = {
title: "hello world",
tags : [1,2,3,4],
showTag(){
this.tags.forEach((tag)=>{
console.log(tag);
console.log(this); //fn
}); //์ฌ๊ธฐ๋ ์ผ๋ฐ ํจ์ ๋ฐ๊นฅ, fn ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์์
}
}
fn.showTag();
// 1
// fn ๊ฐ์ฒด ์ถ๋ ฅ
// 2
// fn ๊ฐ์ฒด ์ถ๋ ฅ
// 3
// fn ๊ฐ์ฒด ์ถ๋ ฅ
// 4
// fn ๊ฐ์ฒด ์ถ๋ ฅ
โ CASE 4. ์ฝ๋ฐฑํจ์
- call, apply, bind
๐ ๋ช ์์ ์ผ๋ก this๋ฅผ ๋ฐ์ธ๋ํ๋ ๋ฐฉ๋ฒ
function a(x,y,z){
console.log(this, x,y,z);
}
var b = {
bb:'bbb'
};
a.call(b, 1,2,3);
a.apply(b, [1,2,3]);
var c = a.bind(b);
c(1,2,3);
var d = a.bind(b, 1,2);
d(3);
//๊ฒฐ๊ณผ
{ bb: 'bbb' } 1 2 3
{ bb: 'bbb' } 1 2 3
{ bb: 'bbb' } 1 2 3
{ bb: 'bbb' } 1 2 3
๐ call : ์ฒซ ๋ฒ์งธ ์ธ์๋ก ๋๊ธด ๊ฒ์ด this, ๋๊ธฐ ๋ ๋ฒ์งธ๋ถํฐ๋ ์ฐจ๋ก๋๋ก ํจ์์ ๋งค๊ฐ๋ณ์๋ก ๋๊น
๐ apply : ์ฒซ ๋ฒ์งธ ์ธ์๋ก ๋๊ธด ๊ฒ์ด this, ๋ฐฐ์ด๋ก ๋๊ธด ๊ฐ๊ฐ์ ์์๋ค์ด ํจ์์ ๋งค๊ฐ๋ณ์๊ฐ ๋จ
๐ ์ฒซ๋ฒ์งธ bind : this๋ฅผ a๋ก ๋๊ฒจ์ฃผ๊ณ , b๋ผ๋ ๋งค๊ฐ๋ณ์๋ฅผ ๋๊ฒจ์ค ์ํ. ์ฌ๊ธฐ์ c๋ผ๋ ๋ฐ์ธ๋๋ ํจ์์ ๋งค๊ฐ๋ณ์ 1, 2, 3์ ๋๊ฒจ์ฃผ๋ฉด ๊ฒฐ๊ณผ๊ฐ์ ๋ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ๊ณผ๋ ๊ฐ๊ฒ ๋ง๋ค ์ ์์
๐ ๋๋ฒ์งธ bind : this๋ฅผ a๋ก ๋๊ฒจ์ฃผ๊ณ , ๋งค๊ฐ๋ณ์๋ก 1, 2๋ฅผ ๋๊ฒจ์ค ์ํ์์ d๋ผ๋ ๋ฐ์ธ๋ ํจ์๋ฅผ ๋ง๋ฆ. ๋ง๋ ํ ๊ทธ ํจ์์ 3์ ๋๊ธฐ๋ฉด์ ๋ค์ ํธ์ถ
๐ป ์์ ์ฝ๋ 1-1)
var callback = function() {
console.log(this);
};
var obj = {
a: 1,
b: function(cb){
cb();
}
}
obj.b(callback);
๐ obj.b์ ํจ์๋ฅผ ํธ์ถํ์ ๋, ๋งค๊ฐ๋ณ์๋ก callback ํจ์๋ฅผ ๋ฐ๋๋ฐ, ์ด callbackํจ์๋ this๋ฅผ ์ถ๋ ฅํ๋ ํจ์
๐ ๊ทธ๋ผ, obj์ ๋ฉ์๋๋ b๋ฅผ ๋ณด๋ฉด, ํจ์ cb๊ฐ cb๋ฅผ ํธ์ถํ๋๋ฐ, ์ฌ๊ธฐ์์ this๋ ๋ฐ๋ก ์ ํด๋์ง ์์์ผ๋ฏ๋ก ( ๋ฉ์๋ ์์ this๋ฅผ ์ ํ ๊ฐ์ด ์๊ธฐ ๋๋ฌธ) callback ํจ์์์ this๋ฅผ ์ถ๋ ฅํ๋ฉด window(์ ์ญ๊ฐ์ฒด)๋ผ๊ณ ๊ฒฐ๊ณผ๊ฐ์ด ๋์ฌ ๊ฒ์ด๋ค
๐ ์ฆ, ๊ทธ๋ฅ ํจ์๋ก์ ํธ์ถํ๋ฉด ๋ฌด์กฐ๊ฑด ์ ์ญ๊ฐ์ฒด
๐ป ์์ ์ฝ๋ 1-2)
var callback = function() {
console.log(this);
};
var obj = {
a: 1,
b: function(cb){
cb.call(this);
}
}
obj.b(callback);
๐ ๋ง์ฝ ์ด ๊ฒฝ์ฐ๋ผ๋ฉด, cb.call(this)์์ this๋ฅผ ๋ฐ๋ก ๋๊ฒจ์ฃผ์์ ๋, this๋ b ๋ฉ์๋ ์์ ์๋ obj ํจ์๊ฐ b ๋ฉ์๋์ ์ฃผ์ฒด์ด๊ธฐ ๋๋ฌธ์ ์ด๋์ this๋ obj๋ก ์ถ๋ ฅ
๐ป ์ฝ๋ 2)
var callback = function(){
console.dir(this); //๋ฐ๋ก this๋ฅผ ์ง์ ํ ๊ฐ์ด ์์ด์ ์ ์ญ๊ฐ์ฒด๋ก ๋์ด
};
var obj = {
a:1
};
setTimeout(callback, 100);
//this์ ๊ฐ์ ์ง์ ํ ๋ ค๋ฉด
setTimeout(callback.bind(this), 100);
๐ setTimeout(callback, 100) : setTimeout ํจ์์์๋ callback ํจ์์ this๊ฐ ๋ฑํ ์ ํด์ง ๊ฒ์ด ์์ด ์ ์ญ๊ฐ์ฒด๊ฐ ๋์ด
๐ setTimeout(callback.bind(this), 100) : callback ํจ์์ this๋ฅผ ์ํ๋ ๊ฐ์ผ๋ก ์ง์ ํ๊ณ ์ถ๋ค๋ฉด, bind๋ฅผ ์ฐ๋ฉด ๋จ
๐ป ์ฝ๋ 3-1)
document.body.innerHTML += '<div id="a"> ํด๋ฆญํ์ธ์ </div';
document.getElementById('a').addEventListener(
'click',
function(){
console.dir(this);
}
);
๐ ์ด๋ this๋ html dome element๊ฐ ๋์ด. ์๋ํ๋ฉด, this๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๊ทธ ํ๊ฒ ๋์ ์๋ฆฌ๋จผํธ๋ก ํ๋๋ก ์ ์๊ฐ ๋์ด์๊ธฐ ๋๋ฌธ์๐ addEventListner์ฒ๋ผ ์ฝ๋ฐฑํจ์์ this๋ฅผ ๋ณ๋๋ก ์ง์ ํด๋์ ๊ฒฝ์ฐ๋ ์ผ๋ง๋ ์ง ์์
๐ป ์ฝ๋ 3-2) ๋ง์ฝ this์ ํน์ obj๋ก ๋ฐ๊พธ๊ณ ์ถ๋ค๋ฉด? bind ์ฝ๋ฐฑํจ์๋ฅผ ์ฌ์ฉํด๋ผ
document.getElementById('a').addEventListener(
'click',
function(){
console.dir(this);
}.bind(obj)
);
โ 5. new ์ฐ์ฐ์
- ์์ฑ์ ํจ์ ๋ฐฉ์์ผ๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ ๊ฒฝ์ฐ
- ์์ฑ์ ํจ์ MyFn๊ฐ ๋น ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ด ์์ฑ์ ํจ์์์ this๊ฐ ๋น๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ์ค์
๐ป ์ฝ๋ 1)
function MyFn(){
this.title = "hello world";
return this;
}
//new ์ฐ์ฐ์๋ฅผ ํตํด ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์ป์
const myfn = new MyFn()
myfn;
//MyFn{title: "hello world"}
๐ป ์ฝ๋ 2)
function Person(n, a){
this.name=n;
this. age = a;
}
var roy = new Person ('์ฌ๋จ', 30); //new
console.log(roy);
console.log(window.name, window.age); //์ฌ๋จ 30
โจ ๊ฒฐ๋ก
- this๋ ์ด์ฒ๋ผ ์ด๋ค ์์น์ ์๋๋, ์ด๋์์ ํธ์ถ๋๋๋, ์ด๋ค ํจ์์ ์๋๋์ ๋ค๋ผ ์ฐธ์กฐ ๊ฐ์ด ๋ฌ๋ผ์ง๋ ํน์ฑ์ด ์์
- ์ ์ด๊ถ์ ๊ฐ์ง ํจ์๋ this๋ฅผ ์ด๋ฏธ ์ง์ ํด๋ ๊ฒฝ์ฐ๋ ์์ (์๋ฅผ๋ค๋ฉด, addEventListner)
-์ด๋ฅผ ์ง์ ๋ฐ๊พธ๊ณ ์ถ๋ค๋ฉด, ๋ฐ์ธ๋ฉ์ ํด๋ผ !