05. this

์–‘ํฌ์ค€ยท2021๋…„ 10์›” 24์ผ
0

JavaScript Info

๋ชฉ๋ก ๋ณด๊ธฐ
5/19
post-thumbnail

๐Ÿ“Œ 5-1 this๋ž€?

์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด, ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋“ฑ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜์ด๋‹ค.

๐Ÿ’ก this๋Š” ํ•จ์ˆ˜ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ์„œ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.
๐Ÿ’ก this์˜ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์ ์— ๊ฒฐ์ •๋œ๋‹ค.

๐Ÿ“Œ 5-2 this ๋ฐ”์ธ๋”ฉ ๋ฐฉ์‹

ํ•จ์ˆ˜ํ˜ธ์ถœ์ด๋‚˜ ๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ์ง€์ •ํ•˜์—ฌ this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค. ์ด๊ฒƒ์„ this ๋ฐ”์ธ๋”ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๐Ÿ’ก ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœ ๋ฐฉ์‹ => window ๊ฐ์ฒด
๐Ÿ’ก ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ์„œ์˜ ํ˜ธ์ถœ => ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด
๐Ÿ’ก ์ƒ์„ฑ์žํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค ํ˜ธ์ถœ => ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด
๐Ÿ’ก ๋ช…์‹œ์  ํ˜ธ์ถœ => function.prototype.[apply, call, bind]
โœ” Doc์—์„œ Node๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ this๋Š” ํ•ด๋‹น Node๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.

๐Ÿ“Œ 5-3 ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ์„œ์˜ this ํ˜ธ์ถœ

function functionThis() {
    return this;
}
console.log(functionThis()); // window ์ถœ๋ ฅ

์ด์™€ ๊ฐ™์ด ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ์„œ์˜ ํ˜ธ์ถœ์˜ this๋Š” ์ „์—ญ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

var a = 10; // ์ „์—ญ๋ณ€์ˆ˜ a ์„ ์–ธ
function functionThis() {
    return this.a;
}
console.log(functionThis()); // 10 ์ถœ๋ ฅ

var๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด window๊ฐ์ฒด์•ˆ์— a๋ฅผ ์„ ์–ธ๋˜์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ 5-4 ๋ฉ”์„œ๋“œ๋กœ์„œ์˜ this ํ˜ธ์ถœ

const obj = {
    objThis : function() {
        return this;
    }
}
console.log(obj.objThis()); // obj ์ถœ๋ ฅ

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ์„œ์˜ ํ˜ธ์ถœ์€ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

const obj = {
    num : 10,
    objThis : function() {
        return this.num;
    }
}
console.log(obj.objThis()); // 10 ์ถœ๋ ฅ

๊ฐ์ฒด์˜ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ 5-5 ์ƒ์„ฑ์žํ•จ์ˆ˜๋กœ์„œ์˜ this ํ˜ธ์ถœ

function createFunction(num) {
    this.num = num;
    this.createFunctionThis = function() {
        return this;
    }
}
const instance1 = new createFunction(10);
const instance2 = new createFunction(20);
console.log(instance1.createFunctionThis()); // instance1 ์ถœ๋ ฅ
console.log(instance2.createFunctionThis()); // instance2 ์ถœ๋ ฅ
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ธํ•ด์„œ ๋งŒ๋“ค์–ด์ง„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ this๋ฅผ ๊ฐ€์ง„๋‹ค.
console.log(instance1 === instance2); // false ์ถœ๋ ฅ

์ƒ์„ฑ์žํ•จ์ˆ˜๋กœ ๋งŒ๋“  ์ธ์Šคํ„ด์Šค๋กœ์„œ์˜ this๋Š” ์ธ์Šคํ„ด์Šค๋Š” ๊ฐ€๋ฆฌํ‚จ๋‹ค.

function createFunction(num) {
    this.num = num;
    this.createFunctionThis = function() {
        return this.num;
    }
}
const instance1 = new createFunction(10);
const instance2 = new createFunction(20);
console.log(instance1.createFunctionThis()); // 10 ์ถœ๋ ฅ
console.log(instance2.createFunctionThis()); // 20 ์ถœ๋ ฅ

์ธ์Šคํ„ด์Šค์˜ ์ธ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ 5-6 this์˜ ๋ช…์‹œ์  ํ˜ธ์ถœ

๐Ÿ“ƒ function.prototype.apply(์‚ฌ์šฉํ•  this ๊ฐ์ฒด, [ํ•จ์ˆ˜์ธ์ž])

const obj = {
    num : 10
}
function applyThis() {
    return this;
}
console.log(applyThis.apply(obj)); // { num : 10 } ์ถœ๋ ฅ

this๊ฐ€ obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋ฐ”์ธ๋”ฉ ๋˜์—ˆ๋‹ค.

const obj = {
    num1 : 10
}
function applyThis(a, b) {
    const num2 = a;
    const num3 = b;
    return this.num1 + num2 + num3;
}
console.log(applyThis.apply(obj, [10, 20])); // 40 ์ถœ๋ ฅ

์ด์ฒ˜๋Ÿผ ์›๋ž˜๋Š” window๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋ฐ”์ธ๋”ฉ๋˜์—ˆ๋‹ค.

๐Ÿ“ƒ function.prototype.call(์‚ฌ์šฉํ•  this ๊ฐ์ฒด, ํ•จ์ˆ˜์ธ์ž)

const obj = {
    num : 10
}
function callThis() {
    return this;
}
console.log(callThis.call(obj)); // { num : 10 } ์ถœ๋ ฅ

this๊ฐ€ obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋ฐ”์ธ๋”ฉ ๋˜์—ˆ๋‹ค.

const obj = {
    num1 : 10
}
function callThis(a, b) {
    const num2 = a;
    const num3 = b;
    return this.num1 + num2 + num3;
}
console.log(callThis.call(obj, 10, -5)); // 15 ์ถœ๋ ฅ

apply์™€ ๋˜‘๊ฐ™์ด ์›๋ž˜๋Š” window๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋ฐ”์ธ๋”ฉ๋˜์—ˆ๋‹ค.

๐Ÿ“ƒ function.prototype.bind(์‚ฌ์šฉํ•  this ๊ฐ์ฒด, ํ•จ์ˆ˜์ธ์ž)

const obj = {
    num : 10
}
function bindThis() {
    return this;
}
console.log(bindThis.bind(obj)); // bindthis ์ถœ๋ ฅ

apply์™€ call๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ bindThis ํ•จ์ˆ˜๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ณ€์ˆ˜์— ๋„ฃ์–ด์ค˜์„œ ์‹คํ–‰์„ ํ•ด์•ผํ•œ๋‹ค.

const obj = {
    num : 10
}
function bindThis() {
    return this;
}
const bindStart = bindThis.bind(obj);
console.log(bindStart()); // { num : 10 } ์ถœ๋ ฅ

์ด์™€ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

const obj = {
    num1 : 10
}
function bindThis(a, b) {
    const num2 = 10;
    const num3 = 20;
    return this.num1 + num2 + num3;
}
const bindStart = bindThis.bind(obj);
console.log(bindStart()); // 40 ์ถœ๋ ฅ

call๊ณผ apply๋Š” ํ•จ์ˆ˜์˜ ์ธ์ž๋ฅผ ๋„ฃ๋Š” ๋ถ€๋ถ„์ด ๋‹ค๋ฅด์ง€๋งŒ this๋ฅผ ํŠน์ •๊ฐ’์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ bind๋Š” ํ•จ์ˆ˜์˜ this์˜ ๊ฐ’์„ ์˜๊ตฌ์ ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Œ 5-7 this์˜ ํ•จ์ˆ˜ํ˜ธ์ถœ ์‹œ์ ์ด๋ž€?

var name = 'KIM'; // window ๊ฐ์ฒด name ์„ ์–ธ
const obj = {
    name : 'LEE',
    info : function(callback) {
      // callback ํ•จ์ˆ˜์˜ ์‹คํ–‰๊ฐ’์„ ๋ฐ˜ํ™˜
        return callback();
    }
}
// callback function
function callback() {
    return `${this.name}์˜ ๋‚˜์ด๋Š” 20์‚ด ์ž…๋‹ˆ๋‹ค.`;
}
// obj์•ˆ์— ์žˆ๋Š” info ๋ฉ”์„œ๋“œ์—์„œ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
console.log(obj.info(callback)); // KIM์˜ ๋‚˜์ด๋Š” 20์‚ด ์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ

์ด์™€ ๊ฐ™์ด ์ž‘๋™ํ•˜๋ฉด window ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜์—ฌ name์„ ์ฝ์–ด์™”๋‹ค. ์™œ๋ƒํ•˜๋ฉด info๋Š” ๋ฉ”์„œ๋“œ๋กœ์„œ์˜ ํ˜ธ์ถœ์ด ๋˜์–ด info์˜ this๋Š” obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€๋งŒ callback์€ info์•ˆ์—์„œ ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ์ด ๋˜์–ด์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ this๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์˜ ์‹คํ–‰์ฒ˜๋Ÿผ window๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.

var name = 'KIM';
const obj = {
    name : 'LEE',
    info : function(callback) {
      // callback ํ•จ์ˆ˜์— bind this๋ฅผ ํ•ด์ค€๋‹ค.
        const bindObj = callback.bind(this);
      // ๋ช…์‹œ์ ์œผ๋กœ ๋ณ€ํ™˜๋œ bindObj๊ฐ’ ์ถœ๋ ฅ
        return bindObj();
    }
}
function callback() {
    return `${this.name}์˜ ๋‚˜์ด๋Š” 20์‚ด ์ž…๋‹ˆ๋‹ค.`;
}
console.log(obj.info(callback)); // LEE์˜ ๋‚˜์ด๋Š” 20์‚ด ์ž…๋‹ˆ๋‹ค.

bind์— this๋งŒ ๋„ฃ์–ด์ค€ ์ด์œ ๋Š” info๋Š” ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ์„œ์˜ ํ˜ธ์ถœ์ด ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— this๊ฐ€ obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค. ๊ทธ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•ด์ฃผ๋ฉด window ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” callback ํ•จ์ˆ˜์— ๋ฐ”์ธ๋”ฉ ํ•˜์—ฌ bindObj ํ•จ์ˆ˜์˜ this๋„ obj๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. ์ด์ฒ˜๋Ÿผ this๋Š” ํ•จ์ˆ˜ํ˜ธ์ถœ ๋ฐฉ์‹๊ณผ ํ˜ธ์ถœ์‹œ์ ์—์„œ ๋™์ ์œผ๋กœ ๋ณ€ํ™”ํ•˜๊ฒŒ ๋œ๋‹ค.

profile
JS ์ฝ”๋ฆฐ์ด

0๊ฐœ์˜ ๋Œ“๊ธ€