Javscript ์์์ this๋ ํจ์๋ฅผ ํธ์ถํ ๋ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด๊ฐ ๊ฒฐ์ ๋๋ค.
ํจ์ ํธ์ถ ๋ฐฉ์๊ณผ this ๋ฐ์ธ๋ฉ
ํจ์ ํธ์ถ
์ผ๋ฐ ํจ์์์ this๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ญ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉ๋๋ค. ๋ฐ๋ผ์ ๋ธ๋ผ์ฐ์ ์์ ์คํ ์ ๋ค์ ํจ์ ์์ this๋ window์ ์ฐ๊ฒฐ๋๋ค. (strict ๋ชจ๋์์๋ undefined). ์ด๋ ๋ฌผ๋ก ๋ด๋ถ ํจ์๋ ๋ง์ฐฌ๊ฐ์ง๋ค
function foo() {
console.log(this); // window
function bar() {
console.log(this) // window
}
bar()
}
foo()์ด๋ ๊ฐ์ฒด์ ๋ฉ์๋ ์์ ๋ด๋ถ ํจ์, ์ฝ๋ฐฑ์ผ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง์ด๋ค.
var obj = {
value: 100,
foo: function() {
console.log("foo's this: ", this); // obj
function bar() { /* ๋ด๋ถํจ์ */
console.log("bar's this: ", this); // window
}
bar();
setTimeout(function() { /* ์ฝ๋ฐฑํจ์ */
console.log("callback's this: ", this); // window
}, 100);
}
};
obj.foo();์ด๋ฌํ ํ์์ ์ผ๋ฐํจ์, ๋ด๋ถํจ์, ์ฝ๋ฐฑ์ this์ ์ง์ ํด์ฃผ์ง ์์๊ธฐ์ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ ๊ฒ์ด๋ค. ์ด๋ JS ๊ฐ๋ฐ์์กฐ์ฐจ ์ค๊ณ์์ ์ค๋ฅ๋ผ๊ณ ํ์ ์ ๋๋ก ์ด์ํ ๋์์ด๋ค
์ด๋ฅผ ํํผํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค
var innerThis = this;์ ๊ฐ์ด ํ์ฌ ๊ฐ์ฒด์ this๋ฅผ ๋ฐ๋ก ์ ์ฅํ์ฌ ์ฌ์ฉcall,bind,apply๋ก this ์ค์ - ํ์ดํ ํจ์ ์ฌ์ฉ
๋ฉ์๋ ํธ์ถ
๊ฐ์ฒด์ ๋ฉ์๋์์ this๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ทธ ๊ฐ์ฒด์ ์ฐ๊ฒฐ์ด ๋๋ค.
var obj = {
a : function {console.log(this)}
}
obj.a(); // objํ์ง๋ง ์๋์ ๊ฐ์ด ๋ฉ์๋๋ฅผ ๊บผ๋ด์จ๋ค๋ฉด ๋ฐ์ธ๋ฉ ๋๋ this๋ ๋ฌ๋ผ์ง๋ค
var a2 = obj.a;
a2() // Window {}this๋ ํธ์ถํ๋ ํจ์์ ๋ฐ๋ผ ๋ฐ์ธ๋ฉ ๋๊ธฐ์, a2๋ฅผ ํธ์ถํ ๊ฒฝ์ฐ a2๋ ์ ์ญ ๊ฐ์ฒด์ ํฌํจ์ด ๋๊ธฐ์ ์ ์ญ ๊ฐ์ฒด์ ์ฐ๋์ด ๋๋ ๊ฒ์ด๋ค.
ํ๋กํ ํ์
ํ๋กํ ํ์ ๊ฐ์ฒด ๋ฉ์๋ ๋ด๋ถ์์ ์ฌ์ฉ๋ this๋ ์ผ๋ฐ ๋ฉ์๋์ฒ๋ผ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ํ๋กํ ํ์ ์ค๋ธ์ ํธ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ค.
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var me = new Person('Lee');
console.log(me.getName()); // "Lee"
Person.prototype.name = 'Kim';
console.log(Person.prototype.getName()); // "Kim"์ฝ๋ฐฑํจ์ ํธ์ถ
์ฝ๋ฐฑ ํจ์๋ ์ ์ญ ๊ฐ์ฒด๋ก ์ฐ๋ ๋๋ค๋ ๊ฒ์ ์์์ ์ดํด๋ดค๋ค. ๋ฐ๋ผ์ ๋ค์ ์์๋ ์ฝ๋ฐฑํจ์๊ฐ ์ ์ญ ๊ฐ์ฒด๋ก ์ฐ๊ฒฐ๋์ด ์ฒซ๋ฒ์งธ ์ฝ์ ๊ฒฐ๊ณผ๊ฐ Not Set์ผ๋ก ๋์ค๊ฒ ๋๋ค.
let userData = {
signUp: '2020-10-06 15:00:00',
id: 'minidoo',
name: 'Not Set',
setName: function(firstName, lastName) {
this.name = firstName + ' ' + lastName;
}
}
function getUserName(firstName, lastName, callback) {
callback(firstName, lastName);
}
getUserName('PARK', 'MINIDDO', userData.setName);
console.log('1: ', userData.name); // Not Set
console.log('2: ', window.name); // PARK MINIDDO์ด๋๋ call, apply, bind๋ก ํด๊ฒฐํ ์ ์๋ค
function getUserName(firstName, lastName, callback) {
callback.call(userData, firstName, lastName);
}
getUserName('PARK', 'MINIDDO', userData.setName);
console.log('1: ', userData.name); // PARK MINIDDO
console.log('2: ', window.name); // undefinedapply/call/bind ํธ์ถ
func.apply(thisArg, [argsArray])
func.call(thisArg, argsArray)
func.bind(thisArg)(argsArray)
// thisArg: ํจ์ ๋ด๋ถ์ this์ ๋ฐ์ธ๋ฉํ ๊ฐ์ฒด
// argsArray: ํจ์์ ์ ๋ฌํ argument์ ๋ฐฐ์ดํธ์ถํ๋ ํจ์์ ๋ฐ์ธ๋ฉํ this๋ฅผ ๋ช ์์ ์ผ๋ก ์ง์ ํ ์ ์์
์ด๋ฒคํธ ๋ฆฌ์ค๋์์์ this
์ด๋ฒคํธ ๋ฆฌ์ค๋์์๋ ๊ทธ ๋ฆฌ์ค๋๊ฐ ๋ถ์ฐฉ๋ dom์ด this๋ก ๋ฐ์ธ๋ฉ๋๋ค.
document.body.onclick = function() {
console.log(this); // <body>
}ํ์ง๋ง ๋ง์ฐฌ๊ฐ์ง๋ก ๋ด๋ถ ํจ์๋ ์ ์ญ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉ๋๋ค.
document.body.onclick = function() {
console.log(this); // body
function inner() {
console.log('inner', this); // inner Window
}
inner();
}์ด ๋ฌธ์ ๋ ์์์ ์ดํด๋ณด์๋ฏ ๋ค์๊ณผ ๊ฐ์ด ํด๊ฒฐํ ์ ์๋ค
var innerThis = this;์ ๊ฐ์ด ํ์ฌ ๊ฐ์ฒด์ this๋ฅผ ๋ฐ๋ก ์ ์ฅํ์ฌ ์ฌ์ฉcall,bind,apply๋ก this ์ค์ - ํ์ดํ ํจ์ ์ฌ์ฉ
์ฒด์ด๋
๊ฐ์ฒด์ ๋ฉ์๋์์ this๋ฅผ ๋ฐํํ๋ฉด ํด๋น this๋ ์๊ธฐ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ธฐ์ ์ฐ์์ ์ผ๋ก ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์ฒด์ด๋์ ํ ์ ์๋ค
let ladder = {
step: 0,
up() {
this.step++;
return this;
},
down() {
this.step--;
return this;
},
}
ladder.up().down().up().down(); ํ์ดํํจ์์ this
ํ์ดํํจ์์๋ this๊ฐ ์กด์ฌํ์ง ์๋๋ค. ๋ฐ๋ผ์ ํ์ดํํจ์์์ this๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ์์ปฌ์ค์ฝํ์ ๋ฐ๋ผ ์์์ค์ฝํ์ ์กด์ฌํ๋ this๋ฅผ ์ฐธ์กฐํ๊ฒ ๋๋ค. ๊ทธ๋ ๊ธฐ์ callback์ด๋ ๋ด๋ถํจ์์์ this๊ฐ ์ ์ญ๊ฐ์ฒด๋ก ์ฐ๊ฒฐ๋๋ ํ์์ ๋ฐฉ์งํ ์ ์๋ค
var obj = {
value: 100,
foo: function() {
console.log("foo's this: ", this); // obj
var bar = () => {
console.log("bar's this: ", this); // obj
}
bar();
setTimeout(() => {
console.log("callback's this: ", this); // obj
}, 100);
}
};
obj.foo();ํ์ง๋ง ์ด๋ฌํ ํน์ฑ ๋๋ฌธ์ ํ์ดํํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฌธ์ ๊ฐ ์๋ ๊ณณ๋ค์ด ์๋ค
- ๋ฉ์๋ : ์์ ์ค์ฝํ์ this๋ฅผ ์ฌ์ฉํ๊ฒ ๋์ด ํ์ฌ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ์ง ๋ชปํ๊ฒ ๋จ.
const obj = {
name : "name",
foo: () => console.log(this.name) // undefined.
}- ์์ฑ์ ํจ์ : ํ์ดํํจ์๋ ์์ฑ์ ํจ์๋ก ์ธ ์ ์์
const Foo = () => {}
const foo = new Foo() // TypeError: Foo is not a constructor- ์ด๋ฒคํธ๋ฆฌ์ค๋ : ์ด๋ฒคํธ๋ฆฌ์ค๋์ this๋ ์ด๋ฒคํธ๊ฐ ๋ถ์ฐฉ๋ ๊ณณ์ ์๋ํ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ ํ์ดํํจ์ ์ฌ์ฉ์ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ถ์ด๋ฒ๋ฆฐ๋ค.
button.addEventListener('click', () => {
console.log(this); // Window
this.innerHTML = 'clicked';
});
document.body.onclick = () => {
console.log(this); // Window
}