์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- cs #๋คํธ์ํฌ
- ์ธ์ฌ์ด๋์์
- ํ๋ก๊ทธ๋๋จธ์ค
- CS
- React.js
- IT๊ฐ๋ฐ์บ ํ
- ์๊ณ ๋ฆฌ์ฆ
- typescript
- ๋ฉ๋ชจ๋ฆฌ
- ํ๋ก์ธ์ค
- ๋ฆฌ์กํธ
- react-query
- ํ์ ์คํฌ๋ฆฝํธ
- ์ ๋ฐ๋ฏธ
- ๋คํธ์ํฌ
- ์ ์ง์ฝํฌ๋น
- BFS
- ์๋ฐ์คํฌ๋ฆฝํธ
- ํด์
- Algorithm
- ํ๋ก์ ํธ์บ ํ
- ์ค๋ ๋
- ๊ฐ๋ฐ์๋ถํธ์บ ํ
- App Runner
- html
- javascript
- #ํ๋ก์ ํธ์บ ํ #ํ๋ก์ ํธ์บ ํํ๊ธฐ #์ ๋ฐ๋ฏธ #์ค๋์ดํผํฉํ ๋ฆฌ #์ ์ง์ฝํฌ๋น #์ธ์ฌ์ด๋์์ #IT๊ฐ๋ฐ์บ ํ #๊ฐ๋ฐ์๋ถํธ์บ ํ #๋ฆฌ์กํธ #react #๋ถํธ์บ ํ #๋ฆฌ์กํธ์บ ํ
- ip
- react
- ์ค๋์ดํผํฉํ ๋ฆฌ
- Today
- Total
Bin's Blog
call, apply, bind ๋ฉ์๋ ๋ณธ๋ฌธ
๐ง ๋ช ์์ ์ผ๋ก this๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ์ธ ๊ฐ์ง๋ฅผ ์ค๋ ์ดํด๋ณด๋ ค๊ณ ํ๋ค.
๋ฐ์ธ๋ฉ์ ๋ญ๊น? ์ฝ๊ฒ ๋งํด์ ๋ฌถ๋๋ค๋ ๋ป์ด๋ค. ํน์ ํ ์ด๋ฆ์ด๋ ๊ฐ์ด๋ ๊ธฐ๋ฅ์ ๋ฌถ์ด์ ์ฌ์ฉํ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค. ์ด๋ค ํค๋ณด๋์ A์ ํน์ ํ ๋์์ธ ์ ํ๋ฅผ ๋ฌถ์ด์ ํ๋ก๊ทธ๋จ์ ๋ง๋ค์๋ค. ์ด์ Aํค๋ฅผ ๋๋ฅด๋ฉด ์ ํํ๋ ๋์์ด ์คํ๋๋ค. ๋จ์ถํค๊ฐ ๋ฐ์ธ๋ฉ์ด๋ค.
1๏ธโฃ call ๋ฉ์๋
- call ๋ฉ์๋๋ ๋ฉ์๋์ ํธ์ถ ์ฃผ์ฒด์ธ ํจ์๋ฅผ ์ฆ์ ์คํํ๋๋ก ํ๋ ๋ช ๋ น์ด๋ค. ์ด๋ call ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ฅผ this๋ก ๋ฐ์ธ๋ฉํ๊ณ , ์ดํ์ ์ธ์๋ค์ ํธ์ถํ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ํ๋ค.
- call ๋ฉ์๋ ์์
let obj = {
a: 1,
method: function (x, y) {
console.log(this.a, x, y);
}
};
// 1, 2, 3
obj.method(2, 3);
// 4, 5, 6
obj.method.call({ a: 4}, 5, 6)
์์์ ๋ดค๋ฏ์ด call์ ์ฒซ ๋ฒ์งธ ์ธ์๊ฐ this๋ก ๋ฐ์ธ๋ฉ์ด ๋์ด์ ์ด์ ์๋ a๊ฐ 1์ ๊ฐ๋ฆฌ์ผฐ๋๋ฐ ์ด์ ๋ this๊ฐ call์ ์ฒซ ๋ฒ์งธ ์ธ์๋ฅผ ๊ฐ๋ฆฌํค๋๊น a๋ 4๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
2๏ธโฃ apply ๋ฉ์๋
- apply ๋ฉ์๋๋ call ๋ฉ์๋์ ๋น์ทํ๋ฐ ์ฐจ์ด๊ฐ ์๋ค๋ฉด ๋ ๋ฒ์งธ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ์ ๊ทธ ๋ฐฐ์ด์ ์์๋ค์ ํธ์ถํ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ์ง์ ํ๋ค.
- apply ๋ฉ์๋ ์์
let obj = {
a: 1,
method: function (x, y) {
console.log(this.a, x, y);
}
};
// 4, 5, 6
obj.method.call({ a: 4 }, 5, 6);
3๏ธโฃ bind ๋ฉ์๋
- bind ๋ฉ์๋๋ ES5์์ ์ถ๊ฐ๋ ๊ธฐ๋ฅ์ผ๋ก, call๊ณผ ๋น์ทํ์ง๋ง ์ฆ์ ํธ์ถํ์ง ์๊ณ ๋๊ฒจ ๋ฐ์ this ๋ฐ ์ธ์๋ค์ ๋ฐํ์ผ๋ก ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๊ธฐ๋ง ํ๋ ๋ฉ์๋์ด๋ค.
- bind ๋ฉ์๋๋ ํจ์์ this๋ฅผ ๋ฏธ๋ฆฌ ์ ์ฉํ๋ ๊ฒ๊ณผ ๋ถ๋ถ ์ ์ฉ ํจ์๋ฅผ ๊ตฌํํ๋ ๋ ๊ฐ์ง ๋ชฉ์ ์ ๋ชจ๋ ์ง๋๋ค.
- bind ๋ฉ์๋ ์์
let func = function(a, b, c, d) {
console.log(this, a, b, c, d);
};
// window{ ... } 1 2 3 4
func(1, 2, 3, 4);
let bindFunc1 = func.bind({ x: 1});
// { x:1 } 5 6 7 8
bindFunc1(5, 6, 7, 8);
let bindFunc2 = func.bind({ x: 1}, 4, 5);
// { x: 1} 4 5 6 7
bindFunc2(6, 7);
// { x: 1} 4 5 8 9
bindFunc2(8, 9);
- ๋ฐ๋ก ์์ ๋ ์ค์์ 6, 7์ ๋๊ธด ๋ถ๋ถ์์ ๋ณด๋ฉด this ๊ฐ์ด ๋ฐ๋ ๊ฒ์ ์ ์ธํ๊ณ ๋ ์ต์ด func ํจ์์ 4, 5, 6, 7์ ๋๊ธด ๊ฒ๊ณผ ๊ฐ์ ๋์์ ํ๋ค.
3๏ธโฃ-1. bind ๋ฉ์๋
- bind ๋ฉ์๋๋ฅผ ์ ์ฉํด์ ์๋ก ๋ง๋ ํจ์๋ ํ ๊ฐ์ง ๋ ํนํ ์ฑ์ง์ด ์๋ค. ๋ฐ๋ก name ํ๋กํผ๋ ๋์ฌ bind์ ์๋ํ์ธ bound ๊ฐ ์ ๋์ด๋ก ๋ถ๋๋ค.
- ์ด๋ค ํจ์์ name ํ๋กํผํฐ๊ฐ "bound xxx"๋ผ๋ฉด ์ด๋ ๊ณง ํจ์๋ช ์ด xxx์ธ ์๋ณธ ํจ์์ bind ๋ฉ์๋๋ฅผ ์ ์ฉํ ์๋ก์ด ํจ์๋ผ๋ ์๋ฏธ๊ฐ ๋๋ฏ๋ก ๊ธฐ์กด์ call์ด๋ apply๋ณด๋ค ์ฝ๋๋ฅผ ์ถ์ ํ๊ธฐ์ ์์ํด์ง ๋ฉด์ด ์๋ค.
let func = function (a, b, c, d) {
console.log(this, a, b, c ,d);
};
let bindFunc = func.bind({ x: 1}, 4, 5);
// func
console.log(func.name);
// bound func
console.log(bindFunc.name);
Ex) bind ๋ฉ์๋ - ๋ด๋ถํจ์์ this ์ ๋ฌ
let obj = {
logThis: function() {
console.log(this);
},
logThisLater1: function() {
setTimeout(this.logThis, 500);
},
logThisLater2: function() {
setTimeout(this.logThis.bind(this), 1000);
},
};
// Window { ... }
obj.logThisLater1();
// obj { logThis: f, ...}
obj.logThisLater2();
4๏ธโฃ ํ์ดํ ํจ์์ ์์ธ์ฌํญ
ES6์ ์๋กญ๊ฒ ๋์ ๋ ํ์ดํ ํจ์๋ ์คํ ์ปจํ ์คํธ ์์ฑ ์ this๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๊ณผ์ ์ด ์ ์ธ๋์๋ค. ์ฆ ์ด ํจ์ ๋ด๋ถ์๋ this๊ฐ ์์ผ๋ฉฐ, ์ ๊ทผํ๊ณ ์ ํ๋ฉด ์ค์ฝํ์ฒด์ธ์ ๊ฐ์ฅ ๊ฐ๊น์ด this์ ์ ๊ทผํ๊ฒ ๋๋ค.
let obj = {
outer: function () {
console.log(this);
let innerFunc = () =? {
console.log(this);
};
innerFunc();
}
};
obj.outer();
5๏ธโฃ ๋ณ๋์ ์ธ์๋ก this๋ฅผ ๋ฐ๋ ๊ฒฝ์ฐ(์ฝ๋ฐฑ ํจ์ ๋ด์์์ this)
Ex) forEach ๋ฉ์๋
let report = {
sum: 0,
count: 0,
add: function() {
let args = Array.prototype.slice.call(arguments);
args.forEach(function (entry) {
this.sum += entry;
++this.count;
}, this);
},
average: function() {
return this.sum / this.count;
}
};
report.add(60, 85, 95);
// 240 3 80
console.log(report.sum, report.count, report.average());
์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์์ this๋ add ๋ฉ์๋์์์ this๊ฐ ์ ๋ฌ๋ ์ํ์ด๋ฏ๋ก add ๋ฉ์๋์ this(report)๋ฅผ ๊ทธ๋๋ก ๊ฐ๋ฆฌํค๊ณ ์๋ค.
6๏ธโฃ ์ฝ๋ฐฑ ํจ์์ ํจ๊ป thisArg๋ฅผ ์ธ์๋ก ๋ฐ๋ ๋ฉ์๋
Array.prototype.forEach(callback[, thisArg])
Array.prototype.map(callback[, thisArg])
Array.prototype.filter(callback[, thisArg])
Array.prototype.some(callback[, thisArg])
Array.prototype.every(callback[, thisArg])
Array.prototype.find(callback[, thisArg])
Array.prototype.findIndex(callback[, thisArg])
Array.prototype.flatMap(callback[, thisArg])
Array.prototype.from(callback[, thisArg])
Set.prototype.forEach(callback[, thisArg])
Map.prototype.forEach(callback[, thisArg])
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
includes ๋ฉ์๋ (0) | 2023.05.26 |
---|---|
some ๋ฉ์๋ (0) | 2023.05.25 |
Symbol (0) | 2023.05.19 |
Promise (0) | 2023.05.18 |
๋น๋๊ธฐ (0) | 2023.05.17 |