๊ฐ์ฒด๋ ์ํ(state)๋ฅผ ๋ํ๋ด๋ ํ๋กํผํฐ(Property)์ ๋์(behavior)์ ๋ํ๋ด๋ ๋ฉ์๋(Method)๋ฅผ ํ๋์ ๋
ผ๋ฆฌ์ ๋จ์๋ก ๋ฌถ์ ๋ณตํฉ์ ์๋ฃ๊ตฌ์กฐ๋ผ ํ์ฃ ? ์ด๋ ๋ฉ์๋๊ฐ ๋ด๋ถ ํ๋กํผํฐ๋ฅผ ์ ์ดํ๊ธฐ ์ํด์ ์์ ์ด ์ํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ฌด์ธ๊ฐ
๊ฐ ์กด์ฌํด์ผ ํฉ๋๋ค. ์ ํฌ๋ ์ด ๋ฌด์ธ๊ฐ์ธ this์ ๋ํด ์์๋ด
๋๋ค.
์๊ธฐ ์ฐธ์กฐ ๋ณ์(self-referencing variable)๋ก ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
์ ๊น ๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํด๋ด ์๋ค. ์ฐ๋ฆฌ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๋ด๋ถ ๋ฉ์๋๋ฅผ ์ ์ํ๋ฉด ๋ฉ์๋ ๋ด๋ถ์์ ์์ ์ด ์ํ ๊ฐ์ฒด์ ์๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์์์ต๋๋ค.
๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐฉ์
์ผ๋ก ์ ์ธํ ๊ฒฝ์ฐconst circle = { // ํ๋กํผํฐ: ๊ฐ์ฒด ๊ณ ์ ์ ์ํ ๋ฐ์ดํฐ radius: 5, // ๋ฉ์๋: ์ํ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๊ณ ์กฐ์ํ๋ ๋์ getDiameter() { // ์์ ์ด ์ํ ๊ฐ์ฒด์ธ circle์ ์ฐธ์กฐํฉ๋๋ค. return 2 * circle.radius; } }; console.log(circle.getDiameter()); // 10
getDiameter
๋ฅผ ํธ์ถํ๋ฉด circle์ ์ฌ๊ท์ ์ผ๋ก ํธ์ถํ์ฃ ? ์ฆ, ์ด ์ฐธ์กฐ ํํ์์ด ํ๊ฐ๋๋ ์์ ์ getDiameter ๋ฉ์๋๊ฐ ํธ์ถ๋์ด ํจ์ ๋ชธ์ฒด๊ฐ ์คํ๋๋ ์์ ์ ๋๋ค.
๊ฐ์ฒด ๋ฆฌํฐ๋ด์ ๋ณ์์ ํ ๋น๋๊ธฐ ์ ์ ํ๊ฐ๋๋ ๊ฒ์ ๊ณ์ ์ธ๊ธํด์์ฃ ? ์ฆ getDiameter๊ฐ ํธ์ถ๋๋ ์์ ์๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ์์ ์ด๋ฏ๋ก ๋ฉ์๋ ๋ด๋ถ์์ ํด๋น ์๋ณ์(์ ์์ ์ ๊ฒฝ์ฐ circle)๋ฅผ ์ฐธ์กฐํ ์ ์๋ ๊ฒ์ด์ฃ . ํ์ง๋ง ๋ด๊ฐ ์ํ ๊ฐ์ฒด๋ฅผ ์ฌ๊ท์ ์ผ๋ก ์ฐธ์กฐํ๋ค๋ ๊ฒ์ ๋น์ฐํ ๊ถ์ฅ๋์ง ์์ต๋๋ค.
์์ฑ์ ํจ์ ๋ฐฉ์
์ผ๋ก ์ ์ธํ ๊ฒฝ์ฐfunction Circle(radius) { // ์ด ์์ ์๋ ์์ฑ์ ํจ์ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๋ณ์๋ฅผ ์ ์ ์์ฃ ? ????.radius = radius; } Circle.prototype.getDiameter = function () { // ์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค. return 2 * ????.radius; }; // ์์ฑ์ ํจ์๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค. const circle = new Circle(5);
- ์์ฑ์ ํจ์ ๋ด๋ถ์์๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ ์ ์์ด์ผ ํฉ๋๋ค. ๊ทธ๋ฌ๋ ์์ฑ์ ํจ์๊ฐ ํธ์ถ๋์ง ์๋ ํ ์ธ์คํด์ค๊ฐ ์์ฑ๋์ง ์์ฃ .
- ๊ฒฐ๊ตญ ์์ฑ์ ํจ์๋ฅผ ์ ์ํ๋ ์์ ์๋ ์ด ์์ฑ์ ํจ์๊ฐ ์ด๋ค ์ธ์คํด์ค๋ฅผ ์์ฑํ๋์ง, ์๋ณ์๋ ๋ฌด์์ธ์ง ์ ์ ์์ต๋๋ค.
๊ฒฐ๊ตญ ์์ ์ด ์ํ ๊ฐ์ฒด๋ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํฌ ์๋ณ์๊ฐ ํ์ํ๋ค๋ ๊ฒ์
๋๋ค. ์ด๊ฒ ๋ฐ๋ก this
์์. ์๊ธฐ ์ฐธ์กฐ ๋ณ์(self-referencing variable)๋ก์ this๋ฅผ ํตํด ํด๋น ๊ฐ์ฒด๋ ์ธ์คํด์ค์ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๊ฒ ๋๋ ๊ฒ์
๋๋ค. ๊ทธ ํน์ง์ ์๋์ ๊ฐ์ต๋๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์ํด ์๋ฌต์ ์ผ๋ก ์์ฑ๋ฉ๋๋ค.
- ๋ฐ๋ผ์ ์ด๋์๋ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
- ํจ์๋ฅผ ํธ์ถํ๋ฉด arguments ๊ฐ์ฒด์ this๊ฐ ์๋ฌต์ ์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค.
- this๋ ์ง์ญ๋ณ์์ฒ๋ผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ,
this ๋ฐ์ธ๋ฉ
์ ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
์์ ์์ ๋ฅผ this๋ก ์์ ํด๋ณผ๊น์?
-
๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐฉ์
์ผ๋ก ์ ์ธํ ๊ฒฝ์ฐconst circle = { // ํ๋กํผํฐ: ๊ฐ์ฒด ๊ณ ์ ์ ์ํ ๋ฐ์ดํฐ radius: 5, // ๋ฉ์๋: ์ํ ๋ฐ์ดํฐ๋ฅผ ์ฐธ์กฐํ๊ณ ์กฐ์ํ๋ ๋์ getDiameter() { // ์์ ์ด ์ํ ๊ฐ์ฒด์ธ circle์ ์ฐธ์กฐํฉ๋๋ค. return 2 * this.radius; } }; console.log(circle.getDiameter()); // 10
-
์์ฑ์ ํจ์ ๋ฐฉ์
์ผ๋ก ์ ์ธํ ๊ฒฝ์ฐfunction Circle(radius) { // ์ด ์์ ์๋ ์์ฑ์ ํจ์ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๋ณ์๋ฅผ ์ ์ ์์ฃ ? this.radius = radius; } Circle.prototype.getDiameter = function () { // ์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค. return 2 * this.radius; }; // ์์ฑ์ ํจ์๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค. const circle = new Circle(5);
this ๋ฐ์ธ๋ฉ์ด๋ ์๋ณ์์ ๊ฐ์ ์ฐ๊ฒฐํ๋ ๊ณผ์ ์ผ๋ก, this(ํค์๋์ด๋ ์๋ณ์๋ก ๋ถ๋ฅ๋ผ์)์ this๊ฐ ๊ฐ๋ฆฌํฌ ๊ฐ์ฒด๋ฅผ ์ฐ๊ฒฐํ๋ ๊ฒ์
๋๋ค. ํด๋์ค ๊ธฐ๋ฐ ์ธ์ด
๋ ์ด this๊ฐ ๊ฐ๋ฆฌํค๋ ๋์์ด ํด๋์ค๊ฐ ์์ฑํ๋ ์ธ์คํด์ค
์ด์ง๋ง ์๋ฐ์คํฌ๋ฆฝํธ๋ ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋ค๋ฅด๋ค๋ ๊ฒ์ ์ ์ํด์ฃผ์ธ์.
strict mode๋ this ๋ฐ์ธ๋ฉ์๋ ์ํฅ์ด ์์ต๋๋ค. ์ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํด์ฃผ์ธ์.
this๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์์
๋๋ค. ์ผ๋ฐ ํจ์ ๋ด๋ถ์์๋ ์ฌ์ฉํ ํ์๊ฐ ์๊ธฐ ๋๋ฌธ์ undefined๊ฐ ํ ๋น๋๋๊ฒ ์ ์(strict mode์ฒ๋ผ์)์ด์ง๋ง ๊ธฐ๋ณธ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์ ์ญ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋ ์ํ์ด๋ฏ๋ก window
๋ฅผ ์ถ๋ ฅํ๋ค๋ ๊ฒ์ ์์๋์ธ์.
์๋ฐ์คํฌ๋ฆฝํธ์์๋ ๋์ผํ ํจ์๋ ๋ค์ํ ๋ฐฉ์์ผ๋ก ํธ์ถํ ์ ์์ต๋๋ค. ํจ์๊ฐ ๋์ผํ๋ฐ ๋ค์ํ ๋ฐฉ์์ผ๋ก ํธ์ถํ ์ ์๋ค๋์...? ๋ฌด์จ ์ข ๋ฅ๊ฐ ์์๊น์? ์ฌ๊ธฐ์๋ ์ข ๋ฅ์ ๋ฐ์ธ๋ฉ์ด ์ด๋ป๊ฒ ๊ฒฐ์ ๋๋์ง ์์ธํ ์์๋ด ๋๋ค.
this ๋ฐ์ธ๋ฉ์ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ ๋์๋์ง์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
์ ๊น! โ ๋ ์์ปฌ ์ค์ฝํ์ this ๋ฐ์ธ๋ฉ์ ๊ฒฐ์ ์๊ธฐ๊ฐ ๋ฌ๋ผ์.
- ๋ ์์ปฌ ์ค์ฝํ(Lexical Scope)๋ ํจ์ ์ ์๊ฐ ํ๊ฐ๋๊ณ ํจ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ ์์ ์ ์์ ์ค์ฝํ๋ฅผ ๊ฒฐ์ ํฉ๋๋ค.
- this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ์์ ์ ๊ฒฐ์ ๋ฉ๋๋ค.
ํจ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ์๊ณผ this ๋ฐ์ธ๋ฉ์ ์๋์ฒ๋ผ ๋ค์ํฉ๋๋ค.
ํจ์ ํธ์ถ ๋ฐฉ์ | this ๋ฐ์ธ๋ฉ |
---|---|
์ผ๋ฐ ํจ์ ํธ์ถ |
์ ์ญ ๊ฐ์ฒด |
๋ฉ์๋ ํธ์ถ |
๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด |
์์ฑ์ ํจ์ ํธ์ถ |
์์ฑ์ ํจ์๊ฐ ๋ฏธ๋์ ์์ฑํ ์ธ์คํด์ค |
Function.prototype.apply/call/bind ๋ฉ์๋์ ์ํ ๊ฐ์ ํธ์ถ |
Function.prototype.apply/call/bind ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ ๊ฐ์ฒด |
๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ญ ๊ฐ์ฒด(global object) ์ this๊ฐ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
์ ์ญ ํจ์์ ๋ด๋ถ ํจ์ ์๊ด ์์ด ์ผ๋ฐ ํจ์๋ก ํธ์ถํ๋ฉด ํจ์ ๋ด๋ถ์ this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค. ๊ทธ๊ฒ์ด ๋ฉ์๋ ๋ด์์ ์ ์ํ ๋ด๋ถ ํจ์๋ ์ฝ๋ฐฑ ํจ์๋ผ๋ ๋์ผํฉ๋๋ค.
// 1. ์ผ๋ฐ ํจ์์ ์ ์ํ ๋ด๋ถ ํจ์
function foo() {
console.log(this); // window
function bar() {
console.log(this); // window
}
"use strict";
function bar2(){
console.log(this); // undefined
}
bar();
}
foo();
// ์ ์ญ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ก์ ํ ๋น๋๋ var ํค์๋
var value = 1;
// 2. ๋ฉ์๋์์ ์ ์ํ ๋ด๋ถ ํจ์
const obj = {
value: 100,
foo() {
console.log(this); // {value: 100, foo: ฦ}
console.log(this.value); // 100
function bar() {
console.log(this); // window
console.log(this.value); // 1
}
bar();
}
};
obj.foo();
// 3. ๋ฉ์๋ ๋ด์ ์ฝ๋ฐฑ ํจ์
const obj = {
value: 100,
foo() {
console.log(this); // {value: 100, foo: ฦ}
setTimeout(function () {
console.log(this); // window
console.log(this.value); // 1
}, 100);
}
};
obj.foo();
๊ฒฐ๊ตญ ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ ๋ชจ๋ ํจ์(๋ด๋ถ, ์ฝ๋ฐฑ ๋ฑ) ๋ด๋ถ์ this๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
๋น์ฐํ this๊ฐ ์ ์ญ ๊ฐ์ฒด์๋ง ๋ฐ์ธ๋ฉ ๋๋ ๊ฒ์ ๋ฌธ์ ๊ฐ ์์ผ๋ฏ๋ก, ๋ฉ์๋์ ๋ด๋ถ ํจ์๋ ์ฝ๋ฐฑ ํจ์์ this ๋ฐ์ธ๋ฉ์ ๋ฉ์๋์ this์ ์ผ์น์ํค๊ธฐ ์ํด์๋ this ์๋ณ์๋ฅผ ๋ณ์์ ํ ๋น
ํ๊ฑฐ๋ apply/call/bind ๋ฉ์๋
, ํ์ดํ ํจ์
๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์.
-
this๋ฅผ ๋ณ์์ ํ ๋นํ๊ธฐ
var value = 1; const obj = { value: 100, foo() { const that = this; setTimeout(function () { console.log(that.value); // 100 }, 100); } }; obj.foo();
-
apply/call/bind ๋ฉ์๋ ์ฌ์ฉํ๊ธฐ
var value = 1; const obj = { value: 100, foo() { setTimeout(function () { console.log(this.value); // 100 }.bind(this), 100); } }; obj.foo();
-
ํ์ดํ ํจ์ ์ฌ์ฉํ๊ธฐ : ํ์ดํ ํจ์์ this๋ ์์ ์ค์ฝํ์ this์ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
var value = 1; const obj = { value: 100, value2 : this.value // 1 foo() { setTimeout(() => console.log(this.value), 100); // 100 } }; obj.foo();
๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๋ง์นจํ ์ฐ์ฐ์ ์์ ๊ธฐ์ ํ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
๋ฉ์๋ ๋ด๋ถ์ this๋ ํ๋กํผํฐ๋ก ๋ฉ์๋๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด์๋ ๊ด๊ณ๊ฐ ์๊ณ , ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ ๋๋ ๊ฒ์ ๋๋ค.
// ๋ฉ์๋ ๋ด๋ถ์ this๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
const person = {
name: 'amy',
getName() {
return this.name;
}
};
// ๋ฉ์๋ getName์ ํธ์ถํ ๊ฐ์ฒด๋ person์ด๋ฏ๋ก
console.log(person.getName()); // amy
const anotherPerson = {
name: 'james'
};
// getName ๋ฉ์๋๋ฅผ anotherPerson ๊ฐ์ฒด์ ๋ฉ์๋๋ก ํ ๋น
anotherPerson.getName = person.getName;
// getName ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ anotherPerson์
๋๋ค.
console.log(anotherPerson.getName()); // james
// getName ๋ฉ์๋๋ฅผ ๋ณ์์ ํ ๋น
const getName = person.getName;
// getName ๋ฉ์๋๋ฅผ ์ผ๋ฐ ํจ์๋ก ํธ์ถ
console.log(getName()); // ''
์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ getName
ํจ์ ๋ด๋ถ์ this.name
์ ์ ์ญ ๊ฐ์ฒด์ ํ๋กํผํฐ window.name
๊ณผ ๊ฐ๊ณ , ๋ธ๋ผ์ฐ์ ์ window.name์ ๋ธ๋ผ์ฐ์ ์ฐฝ์ ์ด๋ฆ์ ๋ํ๋ด๋ ๋นํธ์ธ ํ๋กํผํฐ์
๋๋ค. ๋ฐ๋ผ์ ๊ธฐ๋ณธ๊ฐ์ ''
์ด ๋ผ์(Node.js๋ undefined
).
์ด๋ ํ๋กํ ํ์ ๋ด๋ถ์ ์ฌ์ฉ๋ this ๋ฐ์ธ๋ฉ๋ ๋์ผํฉ๋๋ค.
function Person(name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
};
const me = new Person('amy');
console.log(me.getName()); // amy
Person.prototype.name = 'james';
console.log(Person.prototype.getName()); // james
์์ฑ์ ํจ์๊ฐ ๋ฏธ๋์ ์์ฑํ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
function Circle(radius) {
this.radius = radius;
this.getDiameter = function () {
return 2 * this.radius;
};
}
const circle1 = new Circle(5);
console.log(circle1.getDiameter()); // 10
const circle2 = new Circle(10);
console.log(circle2.getDiameter()); // 20
๋จ, new ์ฐ์ฐ์ ์์ด ํธ์ถํ๋ฉด ์ผ๋ฐ ํจ์์ด๋ฏ๋ก... ์ด๋ป๊ฒ ๋์ํ๊ฒ ๋๋์ง ์์๋์์ฃ ?
Function.prototype์ ๋ฉ์๋๋ก ๋ชจ๋ ํจ์๊ฐ ์์๋ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
apply, call์ this๋ก ์ฌ์ฉํ ๊ฐ์ฒด์ ์ธ์ ๋ฆฌ์คํธ
๋ฅผ, bind๋ this๋ง
์ ์ธ์๋ก ๋ฐ์ต๋๋ค.
-
apply :
Function.prototype.apply(thisArg[, argsArray])
- thisArg : this๋ก ์ฌ์ฉํ ๊ฐ์ฒด
- argsArray : ํจ์์ ์ ๋ฌํ ์ธ์ ๋ฆฌ์คํธ
-
call :
Function.prototype.call(thisArg[, arg1[, arg2[, ...]]])
- thisArg : this๋ก ์ฌ์ฉํ ๊ฐ์ฒด
- arg1, arg2, ... : ํจ์์ ์ ๋ฌํ ์ธ์ ๋ฆฌ์คํธ
-
bind :
Function.prototype.bind(thisArg)
- thisArg : this๋ก ์ฌ์ฉํ ๊ฐ์ฒด
์์ธํ ์ฌ์ฉ๋ฒ์ MDN์ ์ฐธ๊ณ ํด์ฃผ์ธ์.
apply์ call ๋ฉ์๋๋ ๋ณธ๋ ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ด์ง๋ง, ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ํน์ ๊ฐ์ฒด๋ฅผ ํธ์ถํ ํจ์์ this์ ๋ฐ์ธ๋ฉํ์ฃ . ์ธ์๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์์ด ๋ค๋ฅผ ๋ฟ์ด์ง ๋์ ๋ฐฉ์์ ๊ฐ์ต๋๋ค.
- ๋ค์์ getThisBinding ํจ์๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์๋ก ์ ๋ฌํ ๊ฐ์ฒด๋ฅผ getThisBinding ํจ์์ this์ ๋ฐ์ธ๋ฉ์ํค๋ ์ฝ๋์
๋๋ค.
function getThisBinding() { console.log(arguments); return this; } // this๋ก ์ฌ์ฉํ ๊ฐ์ฒด const thisArg = { a: 1 }; // 1. apply ๋ฉ์๋๋ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฌถ์ด ์ ๋ฌ console.log(getThisBinding.apply(thisArg, [1, 2, 3])); // Arguments(3) [1, 2, 3, callee: ฦ, Symbol(Symbol.iterator): ฦ] // {a: 1} // 2. call ๋ฉ์๋๋ ํธ์ถํ ํจ์์ ์ธ์๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ ๋ฆฌ์คํธ ํ์์ผ๋ก ์ ๋ฌ console.log(getThisBinding.call(thisArg, 1, 2, 3)); // Arguments(3) [1, 2, 3, callee: ฦ, Symbol(Symbol.iterator): ฦ] // {a: 1}
- ์ด๋ค์ ๋ํ์ ์ธ ์ฉ๋๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์
๋๋ค.
// 1. arguments ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํ function convertArgsToArray() { console.log(arguments); // Array.prototype.slice๋ฅผ ์ธ์์์ด ํธ์ถํ๋ฉด ๋ฐฐ์ด์ ๋ณต์ฌ๋ณธ์ ์์ฑ const arr = Array.prototype.slice.call(arguments); // const arr = Array.prototype.slice.apply(arguments); console.log(arr); return arr; } convertArgsToArray(1, 2, 3); // [1, 2, 3]
- ์ด๋ค์ ๋ํ์ ์ธ ์ฉ๋๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์
๋๋ค.
bind ๋ฉ์๋๋ ๋ฉ์๋์ this์ ๋ฉ์๋์ ๋ด๋ถ/์ฝ๋ฐฑ ํจ์์ this๊ฐ ๋ถ์ผ์นํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ฉ๋๋ค.
- ๋ค์์ bind ๋ฉ์๋๋ก this๋ฅผ ์ผ์น์ํค๋ ์ฝ๋์
๋๋ค.
const person = { name: 'amy', foo(callback) { // bind ๋ฉ์๋๋ก callback ํจ์ ๋ด๋ถ์ this ๋ฐ์ธ๋ฉ ์ ๋ฌ setTimeout(callback.bind(this), 100); } }; person.foo(function () { console.log(`Hi! ${this.name}.`); // Hi! amy. });