๊ฐ์ฒด๋ ์ํ๋ฅผ ๋ํ๋ด๋ property์ ๋์์ ๋ํ๋ด๋ method๋ก ์ด๋ฃจ์ด์ ธ์๋ค.
๋ฉ์๋๊ฐ ์์ ์ด ์ํ ๊ฐ์ฒด์ property๋ฅผ ์ฐธ์กฐํ๋ ค๋ฉด
const someone = {
name: '9yu',
getName() {
return someone.name;
};
};
์ด๋ ๊ฒ ํ ์ ๋ ์๊ฒ ์ง๋ง, ์ข์ ๋ฐฉ๋ฒ์ ์๋๋ค.
const someone = {
name: '9yu',
getName() {
return this.name;
};
};
๋์ ์ด๋ ๊ฒ this๋ฅผ ์ฌ์ฉํด์ ์์ ์ด ์ํ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
1. this ๋ฐ์ธ๋ฉ
function printThis() {
console.log(this);
}
printThis();
์ง๋์ฃผ ํด๋ก์ ์์ ์คํ์ปจํ ์คํธ์ ๋ํด์ ํ๋ฒ ๋ดค์๋ค. ํจ์๋ฅผ ํธ์ถํ ๋ ์คํ์ปจํ ์คํธ๊ฐ ์์ฑ๋๋๋ฐ, this์ ๋ํ ์ ๋ณด๊ฐ ์ด๋ ์คํ์ปจํ ์คํธ์ ๋ค์ด๊ฐ๋ค. ์ฆ, this๋ ํจ์๋ฅผ ํธ์ถํ ๋ ๊ฒฐ์ ๋๋ค.
this์ this๊ฐ ๊ฐ๋ฆฌํฌ ๊ฐ์ฒด๋ฅผ ์ฐ๊ฒฐํ๋ ๊ฒ์ this ๋ฐ์ธ๋ฉ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ํจ์ ํธ์ถ๋ฐฉ์์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋ฐ์ธ๋ฉ ๋๋ค.
- ์ผ๋ฐ ํจ์ ํธ์ถ : ๊ธฐ๋ณธ ๋ฐ์ธ๋ฉ
- ๋ฉ์๋ ํธ์ถ : ์์์ ๋ฐ์ธ๋ฉ
- ์์ฑ์ ํจ์ ํธ์ถ : new ๋ฐ์ธ๋ฉ
- Function.prototype.apply/call/bind ๋ฉ์๋์ ์ํ ๊ฐ์ ํธ์ถ : ๋ช ์์ ๋ฐ์ธ๋ฉ
a. ๊ธฐ๋ณธ ๋ฐ์ธ๋ฉ
function printThis() {
console.log(this);
}
printThis();
- ์ผ๋ฐํจ์ ํธ์ถ์ this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋๋ค.
- ๋ธ๋ผ์ฐ์ ์ ์ ์ญ๊ฐ์ฒด : window
node์ ์ ์ญ๊ฐ์ฒด : global - ๋ค๋ง ์๊ฒฉ๋ชจ๋์์๋ undefined๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
'use strict';
const obj = {
foo() {
function boo() {
console.log(this);
}
boo();
};
}
obj.foo();
//------------
const obj = {
foo() {
setTimeout(function () {
console.log(this);
}, 0);
};
}
obj.foo();
์ค์ฒฉํจ์์ ์ฝ๋ฐฑํจ์๋ก ์ผ๋ฐํจ์๋ฅผ ์ ์ธํ๊ธฐ ๋๋ฌธ์ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋๋ค. ๋ง์ฝ์ ์ค์ฒฉ์ด๋ ์ฝ๋ฐฑํจ์๋ฅผ ์ด์ฉํด์ this๋ฅผ ์ฌ์ฉํด์ ์์ ์ด ์ํ ๊ฐ์ฒด์ ๋ฉ์๋์ ์ ๊ทผํ๋ ค๊ณ ํ ์ํฉ์์๋ ์๋์ ๋ค๋ฅด๊ฒ ๋ฐ์ธ๋ฉ์ด ๋ ๊ฒ์ด๋ผ๊ณ ํ ์ ์๋ค.
- ๊ถ์ฅํ์ง ์๋ ๋ฐฉ๋ฒ : this๋ก ์ฌ์ฉํ๊ณ ์ ํ๋ ๊ฐ์ฒด๋ฅผ ๋ณ์์ ํ ๋นํ์ฌ ์ฌ์ฉ
const me = {
name : '9yu',
printName() {
const self = this;
setTimout(function () {
console.log(self.name);
},0);
};
};
self๋ผ๋ ์ด๋ฆ์ผ๋ก ๋ณ์๋ฅผ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ค. self์๋ me๊ฐ ๋ค์ด๊ฐ๋ค.
b. ์์์ ๋ฐ์ธ๋ฉ
const outer = {
inner: {
printThis() {
console.log(this);
};
};
};
outer.inner.printThis();
ํจ์๋ฅผ ๊ฐ์ฒด์ ๋ฉ์๋๋ก ํธ์ถํ ๊ฒฝ์ฐ, this๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด, ์ฆ ์ (.) ๋ฐ๋ก ์์ ์ ํ ๊ฐ์ฒด๋ก ๋ฐ์ธ๋ฉ ๋๋ค.
์ฌ๊ธฐ์ this๋ inner๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
c. new ๋ฐ์ธ๋ฉ
function Someone(name) {
this.name = name;
};
const me = new Someone('9yu');
์์ฑ์ ํจ์๋ก ํธ์ถํ ๊ฒฝ์ฐ, ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑํ ์ธ์คํด์ค(me)๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
d. ๋ช ์์ ๋ฐ์ธ๋ฉ
apply, call, bind์ ์ํ ๊ฐ์ ํธ์ถํ ๊ฒฝ์ฐ์ด๋ค.
function.prototype์ ๋ฉ์๋๋ก์, ๋ชจ๋ ํจ์๊ฐ ์์๋ฐ์ ์ฌ์ฉํ ์ ์๋ค.
Function.prototype.apply(thisArg, argsArray)
Function.prototype.call(thisArg, arg1, arg2, ...)
apply
์ call
์ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์๋ง ๋ค๋ฅผ ๋ฟ this๋ก ์ฌ์ฉํ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ฉด์ ํจ์๋ฅผ ํธ์ถํด์ค๋ค.
Function.prototype.bind(thisArg, arg1, arg2, ...)
bind
๋ ์์ ๋๊ฐ์ด this๋ก ์ฌ์ฉํ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ค. ํ์ง๋ง ํจ์๋ฅผ ํธ์ถํด์ฃผ์ง ์๊ณ , thisArg๊ฐ ๋ฐ์ธ๋ฉ๋ ํจ์๊ฐ ๋ฐํ๋๋ค. ์ง์ ํธ์ถํด์ฃผ๋ ์ฝ๋๊ฐ ํ์ํ๋ค.
this ๋ฐ์ธ๋ฉ ๊ท์น ์ฐ์ ์์
์ฌ๋ฌ ๋ฐ์ธ๋ฉ ๊ท์น์ด ์ถฉ๋ํ ๊ฒฝ์ฐ
- new > ๋ช ์์ > ์์์ > ๊ธฐ๋ณธ
2. ํ์ดํ ํจ์์์์ this
const arrowFn = () => {
console.log('๋๋์ผ ํ์ดํ ํจ์');
};
์ผ๋ฐํจ์ ๋ด๋ถ์์ this๊ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ๋ฐ๋ผ๋ณด๋ ํ์์ ํด๊ฒฐํ๊ธฐ ์ํด ES6์์ ๋ฑ์ฅํ๋ค. ํ์ดํํจ์๋ ์คํ์ปจํ ์คํธ๊ฐ ๋ง๋ค์ด์ง ๋ this๋ฅผ ๋ฐ์ธ๋ฉํ์ง ์๋๋ค. ์ฆ, ํ์ดํ ํจ์ ๋ด๋ถ์ this๋ ์กด์ฌํ์ง ์๋๋ค.
- this ํธ์ถ์, ์์ ์ค์ฝํ์ this์ ์ ๊ทผํ๋ค.
- ์ด๋ฅผ Lexical this๋ผ๊ณ ๋ถ๋ฅธ๋ค.
- ๋ช ์์ ๋ฐ์ธ๋ฉ์ ํด์ฃผ๋๋ผ๋ ํ์ดํ ํจ์ ๋ด๋ถ์ ์ฌ์ฉํ๋ this๋ ๊ต์ฒด๋ ์ ์๋ค.
const someone = {
name: '9yu'.
printName: () => {
console.log(this.name);
},
};
someone.printName();
๋ธ๋ผ์ฐ์ ์์ ๋น ๋ฌธ์์ด์ด, ๋ ธ๋์์ undefined๊ฐ ์ถ๋ ฅ๋๋ค. ๋ธ๋ผ์ฐ์ ์์ ์๋์ฐ ๊ฐ์ฒด์ name๊ฐ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ๋น ๋ฌธ์์ด๋ก ์ค์ ๋์ด ์๋ค. ์ฆ, this๊ฐ someone์ด ์๋๋ผ ์ ์ญ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋์ด์๋ค.
- ์ค์ฝํ๋ฅผ ํ๊ณ ํ๊ณ ์ฌ๋ผ๊ฐ์, printName -> someone, someone -> ์ ์ญ๊ฐ์ฒด
- someone์ ๊ฐ๋ฆฌํค๊ณ ์ถ๋ค๋ฉด ํ์ดํํจ์๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค.
3. ํด๋์ค์์์ this
class SomeClass {
//'strict mode;'
}
์ผ๋ฐ์ ์ผ๋ก ๋ด๋ถ์์ this ํธ์ถ ์, ํด๋์ค๊ฐ ์์ฑํ ์ธ์คํด์ค ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ํด๋์ค ๋ด์ ๋ชจ๋ ์ฝ๋์๋ ์๋ฌต์ ์ผ๋ก strict mode๊ฐ ์ ์ฉ๋๋ค. ๋ง์ฝ ๋ฉ์๋ ๋ด๋ถ์ ์ค์ฒฉ์ด๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ผ๋ฐ ํจ์๋ก ์ ์ธํ์๋, ์๊ฒฉ๋ชจ๋๋๊น undefined๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
์ ๋ฆฌ
(์ฐ์ ์์) this ๋ฐ์ธ๋ฉ ๊ท์น
- (4) ์ผ๋ฐํจ์ํธ์ถ(๊ธฐ๋ณธ๋ฐ์ธ๋ฉ) : ์ ์ญ ๊ฐ์ฒด or undefined
- (3) ๋ฉ์๋ ํธ์ถ (์์์ ๋ฐ์ธ๋ฉ) : ํธ์ถ ๊ฐ์ฒด
- (1) ์์ฑ์ํจ์ ํธ์ถ (new ๋ฐ์ธ๋ฉ) : ์ธ์คํด์ค ๊ฐ์ฒด
- (2) Apply, call, bind์ ์ํ ๊ฐ์ ํธ์ถ (๋ช ์์ ๋ฐ์ธ๋ฉ) : ์ฒซ๋ฒ์งธ ์ธ์ ๊ฐ์ฒด
ํ์ดํ ํจ์
- this๋ฅผ ๋ฐ์ธ๋ฉ ํ์ง ์์
- ์์ ์ค์ฝํ์ this์ ์ ๊ทผ
Class
- ๋ด๋ถ : ์๊ฒฉ ๋ชจ๋
- ์ผ๋ฐ์ ์ผ๋ก this => ์ธ์คํด์ค ๊ฐ์ฒด
- ์ผ๋ฐํจ์ ์ ์ธ, ํธ์ถ์ -> undefined