youngdeok 2023. 4. 23. 18:20

“๊ฐ์ฒด”๋Š” ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜์˜ ๋…ผ๋ฆฌ์ ์ธ ๋‹จ์œ„๋กœ ๋ฌถ์€ ๋ณตํ•ฉ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ์ƒํƒœ, ์ฆ‰ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด๋–„ ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ์ด ์†ํ•œ ๊ฐํŽ˜์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐ ํ•˜๋ ค๋ฉด

๋จผ์ € ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

const circle = {
    radius: 5,
    getDiameter() {
        return 2 * Circle.radius
    }
};

console.log(circle.getDiameter()) // 10

์œ„์˜ ์„ค๋ช…์„ ๋“ฃ๊ณ  ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋ฌด์Šจ๋ง์ธ์ง€ ์ดํ•ด ๋˜์‹œ๋‚˜์—ฌ?

  • ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ณ€์ˆ˜(์ƒํƒœ)๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” ์ผ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฐ๋ฐ ๊ฐ์ฒด์•ˆ์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋ณ€์ˆ˜์˜ ์ ‘๊ทผ์„ ํ•˜๊ธฐ์œ„ํ•ด์„œ๋Š” ์ž์‹ ์„ ์žฌ๊ท€์ ์œผ๋กœ ์ฐธ์กฐํ•ด์•ผํ•˜๋Š” ์ผ์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด this ํ‚ค์›Œ๋“œ ์ž…๋‹ˆ๋‹ค!.

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

ํ˜ธ์ถœ ๋ฐฉ์‹์€ ํฌ๊ฒŒ 3๊ฐ€์ง€ ๊ฒฝ์šฐ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

  1. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ
  2. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
  3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ

์ผ๋ฐ˜ ํ•จ์ˆ˜ํ˜ธ์ถœ

๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ์˜ this๋Š” window ์ฆ‰, ์ „์—ญ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋ฉ๋‹ˆ๋‹ค.

function foo() {
    console.log(this) //window
}

๋ฉ”์„œ๋“œ์•ˆ์˜ ์ค‘์ฒฉํ•จ์ˆ˜๋กœ ์„ ์–ธ๋œ ์ผ๋ฐ˜ํ•จ์ˆ˜ ์—ญ์‹œ this๋Š” ์ „์—ญ๊ฐ์ฒด๊ฐ€ ๋ฐ˜์ธ๋”ฉ ๋ฉ๋‹ˆ๋‹ค.

const obj = {
    value: 100,
    foo() {
        console.log('foo`s this: ', this); //window
        console.log('foo`s this.value: ', this.value); // 100
        function bar() {
            console.log('callback`s this', this) //window
            console.log('callback`s this.value', this.value) //1
        };
    }
}

์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ ๋œ๋‹ค๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this ์—ญ์‹œ ์ „์—ญ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋ฉ๋‹ˆ๋‹ค.

const obj = {
    value: 100,
    foo() {
        console.log('foo`s this: ', this) //window
        setTimeout(() => {
            console.log('callback`s this', this) //window
            console.log('callback`s this.value', this.value) //1
        }, 100);
   

์ด์ฒ˜๋Ÿผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ ๋ชจ๋“  ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜์ธ๋”ฉ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ซŒ ์–ด์ƒ‰ํ•˜์ง€ ์•Š๋‚˜์š”???

์ค‘์ฒฉํ•จ์ˆ˜๋‚˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜๋ฅผ ๋•๋Š” ํ—ฌํผํ•จ์ˆ˜์ธ๋ฐ, this๊ฐ€ ์™ธ๋ถ€ํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ํ•ฉ๋‹นํ•˜์ง€ ์•Š์„๊นŒ์š”?

๊ทธ๋Ÿฐ ์ƒ๊ฐ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋’ค์— ์„ค๋ช…ํ•  apply, call, bind์ž…๋‹ˆ๋‹ค.

const obj = {
    value: 100,
    foo() {
        const that = this;
        setTimeout(() => {
            console.log('callback`s this.value', that.value) //100
        }, 100);
    }
}

์ด๋Ÿฐ์‹์˜ ํ…Œํฌ๋‹‰๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ

๋ฉ”์„œ๋“œ์˜ this๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์‹ ์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ต๋‹ˆ๋‹ค.

const person = {
    name: 'lee',
    getName() {
        return this.name;
    }
};

console.log(person.getName()); //lee

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋ฉ๋‹ˆ๋‹ค.

์ฆ‰, ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค..

function Circle(radius) {
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius;
  };
}

const circle1 = new Circle(5);
console.log(circle1.getDiameter()); //10ใ…‡

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ newํ‚ค์›Œ๋“œ ์—†์ด ์ƒ์„ฑํ•œ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜๊ฐ€ ๋˜๋Š”๋ฐ, ๊ทธ๋Ÿด๊ฒฝ์šฐ ์ „์—ญ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ต๋‹ˆ๋‹ค.


Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ‘์ ‘ ํ˜ธ์ถœ

์ค‘์ฒฉ ํ•จ์ˆ˜๋‚˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋ฉ”์„œ๋“œ ๋‚ด์— ์žˆ์œผ๋ฉด ์™ธ๋ถ€ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์—ญ์„ ๊ฐ€๋ฅดํ‚จ๋‹ค๊ณ  ์„ค๋ช… ๋“œ๋ ธ์ฃ ?.

๋˜ ๊ทธ๊ฒŒ ๊ต‰์žฅํžˆ ์–ด์ƒ‰ํ•œ ๊ฒƒ์ด๋ผ๊ณ ๋„ ์„ค๋ช… ๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

์ด์ œ ๊ทธ ์–ด์ƒ‰ํ•จ์„ ํ’€์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

appply, call

๋‘˜์˜ ์‚ฌ์šฉ๋ฒ•์€ ์‚ฌ์‹ค์ƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค.

๋‘ ๋ฉ”์„œ๋“œ๋Š” this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด์™€ ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ(arguments)๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›์•„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ๋ฒ•:

function getThisBinding() {
    return this;
}

const thisArg = {a: 1};
console.log(getThisBinding()) //window
console.log(getThisBinding.apply(thisArg)) // {a: 1}
console.log(getThisBinding.call(thisArg)) // {a: 1}

์‚ฌ์šฉ๋ฒ•์ด ๊ฐ„๋‹จํ•˜์ฃ ?

์ธ์ˆ˜๋ฅผ ๋„ฃ์–ด ์ฃผ๊ณ  ์‹ถ์„ ๋•. ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

function getThisBinding() {
    return this;
}

const thisArg = {a: 1};
console.log(getThisBinding()) //window
console.log(getThisBinding.apply(thisArg, [1, 2, 3]))
console.log(getThisBinding.call(thisArg, 1, 2, 3))๋Ÿฐ

as

bind

bind ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ์˜ this์™€ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ ์ค‘์ฒฉํ•จ์ˆ˜ ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๊ฐ€ ๋ถˆ์ผ์น˜ ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•˜๊ธฐ ์œ„ํ•ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

const person = {
    name: 'lee',
    foo(callback) {
        setTimeout(callback, 100);
    }
}
person.foo(function () {
    console.log(`name: ${this.name}.`); // name: .
    // window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ’์€ ''์ž…๋‹ˆ๋‹ค.
});
const person = {
    name: 'lee',
    foo(callback) {
        setTimeout(callback.bind(this), 100);
    }
}
person.foo(function () {
    console.log(`name: ${this.name}.`); // name: lee.
    // window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ’์€ ''์ž…๋‹ˆ๋‹ค.
};

์ด๋Ÿฐ ์‹์œผ๋กœ ๋ง์ด์ฃ !.

๋‹ค๋ฅธ ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด์™€ ๋‹ฌ๋ฆฌ js์˜ this๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ฐธ ๋™์ ์œผ๋กœ ๋ณ€ํ•ด ์–ด๋ ค์›€์„ ์ค๋‹ˆ๋‹ค.

์ด๊ฒŒ js์˜ ๋งค๋ ฅ์ธ๊ฐ€ ์‹ถ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ด ์ •๋ฆฌ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋”ฅ๋‹ค์ด๋ธŒ๋ฅผ ๋ฐฐ๊ฒฝ์œผ๋กœ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„์›€์ด ๋งŽ์ด ๋˜๋Š” ์ฑ…์ด๊ธฐ์— js๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด ๊ผญ ๋ณด์‹œ๊ธธ~~.

๋˜ this์— ๋Œ€ํ•œ ์žฌ๋ฐŒ๋Š” ์ •๋ฆฌ๊ฐ€ ์žˆ๋Š”๋ฐ ์‹œ๊ฐ„์ด ์žˆ์œผ์‹œ๋ฉด ๊ผฎใ„ฒใ„ฒ ๋ณด์„ธ์—ฌ

https://medium.com/@limsungmook/์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š”-์™œ-ํ”„๋กœํ† ํƒ€์ž…์„-์„ ํƒํ–ˆ์„๊นŒ-997f985adb42

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์™œ ํ”„๋กœํ† ํƒ€์ž…์„ ์„ ํƒํ–ˆ์„๊นŒ

ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋ฉด ์œผ๋ ˆ ๋‚˜์˜ค๋Š” ์„œ๋‘์ฒ˜๋Ÿผ ์ € ๋˜ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฒ˜์Œ ์ ‘ํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ๋‹นํ™ฉ์Šค๋Ÿฌ์› ๋˜ ๊ฒŒ ํ”„๋กœํ† ํƒ€์ž…์ด์—ˆ์Šต๋‹ˆ๋‹ค.

medium.com