์๋ฐ์คํฌ๋ฆฝํธ์์ this
๋ ํท๊ฐ๋ฆฌ๊ธฐ ์ฌ์ด ํค์๋์ด๋ค.
์ ์ฌ์ฉํ๋์ง, ๋ฌด์์ ๊ฐ๋ฆฌํค๋์ง ์ดํดํ๊ธฐ๊ฐ ์ด๋ ต๋ค.
์ด๋ฒ๊ธ์์๋ this
์ ๋ํด ๊ฐ๋
๋ถํฐ ๋ฐ์ธ๋ฉ ๊ท์น๊น์ง ์ ๋ฆฌํ ๊ฒ์ด๋ค.
๐ ํท๊ฐ๋ฆฌ๋ ์ฉ์ด ์ ๋ฆฌ
1๏ธโฃ ๊ฐ์ฒด ๋ฆฌํฐ๋ด
const person = {
name: "Lee",
age: 25
};
์ค๊ดํธ{}
๋ฅผ ์ฌ์ฉํด์ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ด๋ค.
์ ์์ ์ฒ๋ผ key(name
, age
)์ ๊ฐ(”Lee”
, 25
)๋ฅผ ์ง์ง์ด
ํ ๋ฒ์ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๋ฐฉ์์ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ด๋ผ๊ณ ํ๋ค.
2๏ธโฃ ์ธ์คํด์ค
function Person(name) {
this.name = name;
}
const me = new Person("Lee"); // me๊ฐ ์ธ์คํด์ค
์ค๊ณ๋ ์ญํ ์ ํ๋ ์์ฑ์ ํจ์๋ ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก
new
ํค์๋๋ฅผ ์ฌ์ฉํด ๋ง๋ค์ด์ง ์ค์ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค.
์์ฑ์ ํจ์๊ฐ ๋ถ์ด๋นต ๊ธฐ๊ณ๋ผ๋ฉด, ์ธ์คํด์ค๋ ๋ถ์ด๋นต์ผ๋ก ๋น์ ํ ์ ์์
3๏ธโฃ ์ฌ๊ท์ ์ฐธ์กฐ
const circle = {
radius: 5,
getDiameter() {
return 2 * circle.radius; // circle์ ๋ค์ ์ฐธ์กฐ
}
};
๊ฐ์ฒด ๋ด๋ถ์ ๋ฉ์๋๊ฐ ์๊ธฐ ์์ ์ด ์ํ ๊ฐ์ฒด๋ฅผ ๋ณ์ ์ด๋ฆ์ผ๋ก ๋ค์ ์ฐธ์กฐํ๋ ๊ฒ์ ๋งํ๋ค.
์ด ๋ฐฉ์์ ๊ฐ์ฒด ์ด๋ฆ์ด ๋ฐ๋๋ฉด ๋ด๋ถ ์ฝ๋๋ ์ ๋ถ ์์ ํด์ผ ํ๋ฏ๋ก ๊ถ์ฅ๋์ง ์๋๋ค.
4๏ธโฃ this ๋ฐ์ธ๋ฉ
this
๊ฐ ์ด๋ค ๊ฐ์ ๊ฐ๋ฆฌํค๋๋ก ์ฐ๊ฒฐ๋๋ ๊ฒ์ this ๋ฐ์ธ๋ฉ์ด๋ผ๊ณ ํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์์๋ this
๊ฐ ์ ์ ์ผ๋ก ๊ณ ์ ๋์ง ์๊ณ ,
ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ this
๊ฐ ๋ฌ๋ผ์ง๋ค.
→ ์ด๋ฅผ ๋์ ๋ฐ์ธ๋ฉ์ด๋ผ๊ณ ๋ ํ๋ค.
โthis๋ ์ ํ์ํ ๊น?
1. ๊ฐ์ฒด ๋ฆฌํฐ๋ด์์์ ํ์์ฑ
const circle = {
radius: 5,
getDiameter() {
return 2 * this.radius;
}
};
console.log(circle.getDiameter()); // 10
์ฌ๊ธฐ์ getDiameter
์์์ this.radius
๋ฅผ ์ฌ์ฉํ๋ค.
๋ง์ฝ this
๋์ circle.radius
๋ผ๊ณ ์์ฑํ๋ค๋ฉด ์ด๋ป๊ฒ ๋์ํ ๊น?
const circle = {
radius: 5,
getDiameter() {
return 2 * circle.radius;
}
};
console.log(circle.getDiameter()); // 10
circle.radius
๋ผ๊ณ ์์ฑํด๋ ์ฝ๋๋ ๋ฌธ์ ์์ด ๋์ํ๋ค.
๊ทธ ์ด์ ๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๋ณ์์ ํ ๋น๋๊ธฐ ์ง์ ์ ํ๊ฐ๋๊ธฐ ๋๋ฌธ์ด๋ค.
getDiameter
๋ฉ์๋๊ฐ ํธ์ถ๋๋ ์์ ์๋ ์ด๋ฏธ ๊ฐ์ฒด๊ฐ ์์ฑ๋์ด ์๊ณ ,
circle
์๋ณ์์ ๊ทธ ๊ฐ์ฒด๊ฐ ํ ๋น๋ ์ํ์ฌ์ circle.radius
๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
ํ์ง๋ง ์ด ๋ฐฉ์์ ์ฌ๊ท์ ์ฐธ์กฐ์ด๋ฏ๋ก ๊ฐ์ฒด ์ด๋ฆ์ด ๋ฐ๋๋ฉด ๋ด๋ถ ์ฝ๋๋ ๋ชจ๋ ์์ ํด์ผ ํ๋ค.
๊ทธ๋์ ์์ ํ๊ฒ ๊ฐ์ฒด ์์ ์ ๊ฐ๋ฆฌํค๋ ค๋ฉด this
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
2. ์์ฑ์ ํจ์๋ฐฉ์
function Circle(radius) {
์ธ์คํด์ค์ฐธ์กฐ๋ถ๊ฐ.radius = radius;
}
Circle.prototype.getDiameter = function () {
return 2 * ์ธ์คํด์ค์ฐธ์กฐ๋ถ๊ฐ.radius;
}
์์ฑ์ ํจ์๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด๊ณผ ๋ค๋ฅด๋ค.
์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ง์ํ ์ ์๋ฏ์ด ์์ฑ์ ํจ์๋ฅผ ์ ์ํ๋ ์์ ์๋
์์ง ์ธ์คํด์ค๊ฐ ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์ ์์ฑ์ ํจ์๊ฐ ๋ง๋ค์ด๋ผ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํฌ ์๋ณ์๊ฐ ์๋ค.
๊ทธ๋์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ํน๋ณํ ์๋ณ์ this
๋ฅผ ์ ๊ณตํ๋ค.
function Circle(radius) {
this.radius = radius;
}
Circle.prototype.getDiameter = function () {
return 2 * this.radius;
}
์ฌ๊ธฐ์ new Circle(5)
๋ฅผ ์คํํ๋ฉด ๋น ๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง๊ณ , this
๊ฐ ๊ทธ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๋ฐ๋ผ์ this.radius = radius
๊ฐ ์ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ก ์ ์ฅ๋๋ ๊ฒ์ด๋ค.
์์ฑ์ ํจ์ ์ ์ ์์ ์๋ ์ธ์คํด์ค๋ฅผ ์ง์ ์ฐธ์กฐํ ์ ์์ผ๋ฏ๋ก this๊ฐ ๋ฐ๋์ ํ์ํ๋ค.
๐ this๋?
this
๋ ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์์ด๋ค.
์ด๋ฅผ ํตํด ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ฑํ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
๐ this ํน์ง
this
๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์ํด ์๋์ผ๋ก ์์ฑ๋๋ค.- ์ด๋์๋ (์ ์ญ, ํจ์ ๋ด๋ถ) ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ง์ญ ๋ณ์์ฒ๋ผ ์ฌ์ฉํ๋ค.
- ํจ์๋ฅผ ์ด๋ป๊ฒ ํธ์ถํ๋๋์ ๋ฐ๋ผ
this
๊ฐ ๋ฌ๋ผ์ง๋ค.- Java ๊ฐ์ ํด๋์ค ๊ธฐ๋ฐ ์ธ์ด์์๋
this
๊ฐ ํญ์ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค. - ํ์ง๋ง JavaScript๋ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ
this
๊ฐ ๋ฐ๋๋ค.
- Java ๊ฐ์ ํด๋์ค ๊ธฐ๋ฐ ์ธ์ด์์๋
strict mode
(์๊ฒฉ ๋ชจ๋)์์๋this
๋ฐ์ธ๋ฉ์ด ๋ฌ๋ผ์ง๋ค.
๐ this์ ๋ฐ์ธ๋ฉ ๊ท์น
๐ ์ ์ญ ๊ณต๊ฐ์์์ this
console.log(this); // ๋ธ๋ผ์ฐ์ : window, Node.js: global
์ฝ๋์ ๊ฐ์ฅ ๋ฐ๊นฅ์ธ ์ ์ญ ๊ณต๊ฐ์์ this
๋ฅผ ํธ์ถํ๋ฉด ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
- ๋ธ๋ผ์ฐ์ ํ๊ฒฝ:
window
๊ฐ์ฒด - Node.js ํ๊ฒฝ:
global
๊ฐ์ฒด
๐ ์ผ๋ฐ ํจ์ ํธ์ถ์์์ this
function hi() {
console.log(this) // window
}
function hello() {
console.log(this) // window
function hello2() {
console.log(this) // window
}
}
์ผ๋ฐ ํจ์์์ this
๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
์ค์ฒฉ ํจ์๋ ๋์ผํ๊ฒ ํจ์ ๋ด๋ถ์ this
๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
this๋ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํ ์๊ธฐ ์ฐธ์กฐ ๋ณ์์ด๊ธฐ ๋๋ฌธ์
๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๋ ์ผ๋ฐ ํจ์์์ this๋ ์๋ฏธ๊ฐ ์์
๐ก strict mode(์๊ฒฉ ๋ชจ๋) ์์ธ
'use strict'
function hi() {
console.log(this) // undefined
}
function hello() {
console.log(this) // undefined
function hello2() {
console.log(this) // undefined
}
}
strict mode
๊ฐ ์ ์ฉ๋ ์ผ๋ฐ ํจ์ ๋ด๋ถ์ this
๋ undefined
๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๐ ์ฝ๋ฐฑ ํจ์ ํธ์ถ์์์ this
function outer(callback) {
callback();
}
outer(function() {
console.log(this); // window
});
์ฝ๋ฐฑ ํจ์๋ ์ผ๋ฐ์ ์ผ๋ก this
๊ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
ํ์ง๋ง ์ฝ๋ฐฑ ํจ์๋ ์ด๋ค ๋ฉ์๋์์ ์ฝ๋ฐฑ ํจ์๋ฅผ ํธ์ถํ๋์ ๋ฐ๋ผ
this
๊ฐ์ด ๋ฌ๋ผ์ง ์ ์์ด์ ์ฃผ์ํด์ผ ํ๋ค.
const numbers = [1, 2, 3];
const obj = { value: 100 };
numbers.forEach(function(num) {
console.log(this.value + num); // ๋ ๋ฒ์งธ ์ธ์ obj๊ฐ this๋ก ์ฌ์ฉ๋จ
}, obj);
// 101, 102, 103
์ ์์ ์ forEach
์ฒ๋ผ this
๊ฐ์ ์ง์ ์ง์ ํ ์ ์๋๋ก
๋ ๋ฒ์งธ ์ธ์๋ฅผ ์ ๊ณตํ๋ ๋ฉ์๋๋ค๋ ์๋ค.
๐ช ๋ฉ์๋ ํธ์ถ์์์ this
๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๊ฐ this
๊ฐ ๋๋ค.
์ฃผ์ > ๋ฉ์๋๋ฅผ ์์ ํ ๊ฐ์ฒด๊ฐ ์๋๋ผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋จ
const person = {
name: 'Lee',
getName() {
return this.name;
}
}
console.log(person.getName()); // Lee
getName
ํ๋กํผํฐ๋ ํจ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ค.
์ด ํจ์ ๊ฐ์ฒด๋ person ๊ฐ์ฒด์ ํฌํจ๋ ๊ฒ์ด ์๋๋ผ ๋ ๋ฆฝ์ ์ผ๋ก ์กด์ฌํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ฉ์๋ ๋ด๋ถ this๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ์๋์ผ๋ก ๋ฐ์ธ๋ฉํ๊ธฐ ๋๋ฌธ์
this๋ person ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
const getName = person.getName;
console.log(getName()); // ''
ํ์ง๋ง getName
๋ฉ์๋๋ฅผ ์ผ๋ฐ ํจ์์ฒ๋ผ ํธ์ถํ๋ฉด
this
๊ฐ ์๋์ฐ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋์ด window.name
์ ๋ฐ์ธ๋ฉํ๋ค.
๊ทธ ์ด์ ๋ this
์ ๋ฐ์ธ๋ฉ๋ ๊ฐ์ฒด๋ ํธ์ถ ์์ ์ ๊ฒฐ์ ๋๊ฒ ๋๋๋ฐ
getName()
์ ์ผ๋ฐ ํจ์์ด๊ธฐ ๋๋ฌธ์ ํธ์ถ ์์ ์ this
๊ฐ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋๋ค.
๊ทธ๋์ this๋ person ๊ฐ์ฒด๊ฐ ์๋๋ผ window ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
๐ ์์ฑ์ ํจ์ ํธ์ถ์์์ this
์์ฑ์ ํจ์์์ this
๋ ์์ฑ์ ํจ์๊ฐ ๋ฏธ๋์ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
→ this
๋ ์๋์ผ๋ก ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ค.
function Circle(radius) {
this.radius = radius;
this.getDiameter = function () {
return 2 * this.radius;
};
}
const circle1 = new Circle(5);
const circle2 = new Circle(10);
console.log(circle1.getDiameter()); // 10
console.log(circle2.getDiameter()); // 20
์ ์ฝ๋์์ new Circle
์ด ํธ์ถ๋๋ฉด
์๋ก์ด ๋น ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ this
๋ ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
๊ฐ์ฒด์ ์์ฑ์ด this.radius
๋ก ๊ฒฐ์ ๋๊ฒ ๋๊ณ ์์ฑ์ ํจ์๊ฐ ์๋ฃ๋ ์ดํ this๊ฐ ๋ฐํ๋๋ค.
๊ทธ๋์ ๊ฐ๊ฐ ์๋ก์ด ๊ฐ์ฒด๋ก ์์ฑ๋๊ฒ ๋๊ณ getDiameter
๊ฐ์ด ๋ฌ๋ผ์ง๊ฒ ๋๋ ๊ฒ์ด๋ค.
โก๏ธ ํ์ดํ ํจ์์์์ this
ํ์ดํ ํจ์๋ ์์ ๋ง์ this
๋ฅผ ๊ฐ์ง ์๋๋ค.
๋์ ์์ ์ด ์ ์ธ๋ ์์น์ this(์์์ค์ฝํ)๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ค.
→ ์ด๋ฅผ Lexical this๋ผ๊ณ ํ๋ค.
ํ์ดํ ํจ์๋ ํธ์ถ ๋ฐฉ์์ ์ํฅ์ ๋ฐ์ง ์๊ณ ,
์ธ์ ์ด๋์ ํธ์ถ๋๋ ์ ์ธ ๋น์์ this๋ฅผ ๊ธฐ์ตํ๋ค๋ ์ ์ด ํฐ ํน์ง์ด๋ค.
const person = {
name: "Lee",
hello: function () {
const inner = () => {
console.log(this.name);
};
inner();
}
};
person.hello(); // Lee
์ ์์ ์ฒ๋ผ ํ์ดํ ํจ์๋ ๋ณ๋์ ๋ฐ์ธ๋ฉ์ ํ์ง ์์๋
์๋์ผ๋ก ์์ ์ค์ฝํ์ธ person
๊ฐ์ฒด์ name
์ ์ฐธ์กฐํ๊ฒ ๋๋ค.
์ด๋ฌํ ํน์ง์ ์ด์ฉํด์
์ค์ฒฉํจ์์์ bind ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค.
๐ this ๊ณ ์ ๋ฐฉ๋ฒ (apply, call, bind)
this
์ ๋ฐ์ธ๋ฉ ๊ท์น์ ๋ณ๊ฒฝํ๊ณ ์ถ์ ๋๋ apply
, call
, bind
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
์ด ๋ฉ์๋๋ค์ Function.prototype
์ ๋ฉ์๋์ฌ์ ๋ชจ๋ ํจ์์์ ์ฌ์ฉํ ์ ์๊ณ ,
this
๋ฅผ ํน์ ๊ฐ์ฒด์ ๊ฐ์ ๋ก ๋ฐ์ธ๋ฉํ๋ค.
1๏ธโฃ apply
& call
๋ฉ์๋
apply
์ call
์ ํจ์๋ฅผ ์ฆ์ ํธ์ถํ๋ฉด์ this
๊ฐ์ ์ํ๋ ๋๋ก ์ค์ ํ๋ค.
apply
: ๋ฉ์๋๋ฅผ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฌถ์ด์ ์ ๋ฌํ๋ค.call
: ๋ฉ์๋๋ฅผ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์ ๋ฌํ๋ค.
function introduce(greeting) {
console.log(`${greeting}, my name is ${this.name}`);
}
const person = { name: "Lee" };
introduce.apply(person, ["Hello"]); // Hello, my name is Lee
introduce.call(person, "Hi"); // Hi, my name is Lee
apply
& call
๋ฉ์๋์ ์ ์ฌ ๋ฐฐ์ด
apply
์ call
๋ฉ์๋๋ ์ฃผ๋ก arguments
๊ฐ์ฒด์ ๊ฐ์ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์
๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํ ๋ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
function argumentsToArray() {
const arr = Array.prototype.slice.call(arguments)
return arr;
}
argumentsToArray(1, 2, 3); // [1, 2, 3]
2๏ธโฃ bind
๋ฉ์๋
bind
๋ apply
, call
๊ณผ ๋ค๋ฅด๊ฒ ์ฆ์ ์คํํ์ง ์๊ณ ,
this
๊ฐ ๊ณ ์ ๋ ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ ๊ฒ์ด ํน์ง์ด๋ค.
function introduce(greeting) {
console.log(`${greeting}, my name is ${this.name}`);
}
const person = { name: "Lee" };
const boundIntroduce = introduce.bind(person);
boundIntroduce("Hey"); // Hey, my name is Lee
โ ์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์์
const person = {
name: "Lee",
sayHello: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
const anotherPerson = { name: "Kim" };
const boundHello = person.sayHello.bind(anotherPerson);
boundHello(); // Hello, my name is Kim
person.sayHello
๋ฅผ ์๋๋ person
์ด ํธ์ถํด์ผ ํ์ง๋ง,
bind
๋ฅผ ํตํด anotherPerson
์ด ํธ์ถํ ๊ฒ์ฒ๋ผ this
๋ฅผ ๋ณ๊ฒฝํ ์๋ ์๋ค.
โ ์ค์ฒฉ ํจ์์์ this ๋ฌธ์ ํด๊ฒฐ
bind
๋ ๋ฉ์๋์ this์ ๋ฉ์ค๋ ๋ด๋ถ์ ์ค์ฒฉ ํจ์ ๋๋ ์ฝ๋ฐฑ ํจ์์ this๊ฐ
๋ถ์ผ์น ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค.
const counter = {
count: 0,
increase: function() {
function inner() {
console.log(this.count);
}
const boundInner = inner.bind(this);
boundInner(); // 0
}
};
counter.increase();
์ผ๋ฐ์ ์ผ๋ก ์ค์ฒฉ ํจ์ inner
์์์ this
๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
ํ์ง๋ง bind(this)
๋ฅผ ํตํด counter
๊ฐ์ฒด์ this
๋ฅผ ๊ฐ์ ๋ก ์ฐ๊ฒฐํ๊ธฐ ๋๋ฌธ์
counter.conunt
๋ฅผ this
๊ฐ ์ฐธ์กฐํ ์ ์๋ค.
๐ ์ ๋ฆฌ
ํธ์ถ ๋ฐฉ์ | this๊ฐ ๊ฐ๋ฆฌํค๋ ๋์ |
์ ์ญ ๊ณต๊ฐ | ๋ธ๋ผ์ฐ์ : window / Node.js: global |
์ผ๋ฐ ํจ์ | ์ ์ญ ๊ฐ์ฒด (strict ๋ชจ๋: undefined) |
๋ฉ์๋ ํธ์ถ | ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด |
์์ฑ์ ํจ์ | ์๋ก ์์ฑ๋ ์ธ์คํด์ค |
ํ์ดํ ํจ์ | ์์ ์ด ์ ์ธ๋ ์์น์ this |
call/apply/bind | ์ง์ ํ ๊ฐ์ฒด๋ก this ๊ณ ์ |
๐ ์ฐธ๊ณ
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive - 22์ฅ this
๐ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ดํ ํจ์ ์ฌ์ฉ๋ฒ ์ด์ ๋ฆฌ
ํ์ดํ ํจ์ (Arrow function) โ์๋ฐ์คํฌ๋ฆฝํธ์์ ํจ์๋ฅผ ๋ง๋ค ๋๋ function ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ํ ์ ์๋ค. ์๋ฅผ๋ค์ด ๋ ์์ ํฉ์ ๊ตฌํ๋ ํจ์๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค. function sum(a, b) { re
inpa.tistory.com
[JS] ๐ ์๋ฐ์คํฌ๋ฆฝํธ this ๐ฏ ์์ ์ ๋ณต
this ์ ์ let group = { title: "1๋ชจ๋ ", students: ["๋ณด๋ผ", "ํธ์ง", "์ง๋ฏผ"], title2 : this.title, title3() { console.log(this.title) } }; console.log(group.title2); //undefined group.title3(); // 1๋ชจ๋ this๋ ํจ์์ ๋ธ๋ก ์ค์ฝํ ๋ด์
inpa.tistory.com
์๋ฐ์คํฌ๋ฆฝํธ์์ ‘this’ ๋ง์คํฐํ๊ธฐ | ์์ฆIT
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ๋ฐฐ์ฐ๋ค ๋ณด๋ฉด, ์ข ์ข this๋ผ๋ ํค์๋๋ฅผ ๋ง์ฃผ์น๊ฒ ๋๋๋ฐ์. ์ธ๋ป ๋ณด๊ธฐ์๋ ๋จ์ํ ์๋จ์ด ‘this’์ ๋ป๊ณผ ๋น์ทํ๊ฒ ํ์ฌ์ ๋ฌธ๋งฅ์ด๋ ๋งฅ๋ฝ์ ๋ํ๋ด๋ ๊ฒ์ฒ๋ผ ๋ณด์ ๋๋ค. ํ์ง๋ง ๋ง
yozm.wishket.com