22์žฅ this

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

console.log(circle.getDiameter()); // 10
  • ์œ„ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ํ‰๊ฐ€๊ฐ€ ์™„๋ฃŒ๋˜์–ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๊ณ , circle ์‹๋ณ„์ž์— ์ƒ์„ฑ๋œ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น๋œ ์ดํ›„์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ circle ์‹๋ณ„์ž๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ํ•˜์ง€๋งŒ ์žฌ๊ท€์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ์‹์€ ์ผ๋ฐ˜์ ์ด์ง€๋„ ์•Š๊ณ  ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๋‹ค.

๐Ÿ’ก ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŠน์ˆ˜ํ•œ ์‹๋ณ„์ž๊ฐ€ ํ•„์š”ํ•˜๊ณ , `this`๊ฐ€ ๋ฐ”๋กœ ๊ทธ ์‹๋ณ„์ž์ด๋‹ค.

22.1 this ํ‚ค์›Œ๋“œ

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋“ค์€ this๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

  • this๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด ๋˜๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜์ด๋‹ค.

  • this๋Š” ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ๋˜๊ณ , ์ฝ”๋“œ ์–ด๋””์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

  • this๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ์“ฐ์ผ ๋•Œ ์˜๋ฏธ๊ฐ€ ์žˆ์ง€๋งŒ ์ „์—ญ์—์„œ๋„ ์ฐธ์กฐํ•  ์ˆ˜๋Š” ์žˆ๋‹ค.

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

  • this๋ฅผ ์ „์—ญ์—์„œ ์ฐธ์กฐํ•  ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.

    • ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋™์ž‘ํ•  ๊ฒฝ์šฐ this๋Š” ์ „์—ญ ๊ฐ์ฒด์ธ window๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

    • node REPL์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด์ธ global์„ ์ฐธ์กฐํ•œ๋‹ค.

    • commonJS์˜ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ๋”ฐ๋ฅด๋Š” node ํ™˜๊ฒฝ์—์„œ๋Š” module.exports๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

    • ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์ผ ๋•Œ, strict mode๋ผ๋ฉด undefined๊ฐ€ ํ• ๋‹น๋œ๋‹ค.

22.2 ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹๊ณผ this ๋ฐ”์ธ๋”ฉ

this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹, ์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.

์ฃผ์˜ํ•  ๊ฒƒ์€ ๋™์ผํ•œ ํ•จ์ˆ˜๋„ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

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

  • ๊ธฐ๋ณธ์ ์œผ๋กœ this์—๋Š” ์ „์—ญ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

  • ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์—์„œ์˜ this๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.

  • strict mode๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

function foo() {
	console.log("foo's this: ", this); //window
	function bar() {
	console.log("bar's this: ", this); //window
	}
	bar();
}

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

  • ํ•˜์ง€๋งŒ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉํ•จ์ˆ˜ ๋˜๋Š” ๋ฉ”์„œ๋“œ์—๊ฒŒ ์ „๋‹ฌํ•œ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋  ๋•Œ ๊ทธ ํ•จ์ˆ˜๋“ค์˜ this๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.

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

ํ•ด๊ฒฐ์ฑ…

  1. this ๋ฐ”์ธ๋”ฉ์„ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ํ• ๋‹น

var value = 1;

const obj = {
  value: 100,
  foo() {
    const that = this;
    setTimeout(function () {
      console.log(that.value);
    }, 100);
  },
};

obj.foo();
  1. ์ฝœ๋ฐฑํ•จ์ˆ˜์— ๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ๋ฐ”์ธ๋”ฉ

var value = 1;

const obj = {
  value: 100,
  foo() {
    setTimeout(function () {
      console.log(this.value);
    }.bind(this), 100);
  },
};

obj.foo();
  1. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์ด์šฉ

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

var value = 1;

const obj = {
  value: 100,
  foo() {
    setTimeout(()=>console.log(this.value), 100);
  }
};

obj.foo();

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

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

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

์œ„ ์˜ˆ์ œ์— getName ๋ฉ”์„œ๋“œ๋Š” person ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ์ •์˜๋˜์—ˆ๋‹ค.

  • ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํผํ‹ฐ์— ๋ฐ”์ธ๋”ฉ๋œ ํ•จ์ˆ˜์ด๊ธฐ์— getNameํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” person์— ํฌํ•จ๋œ ๊ฒƒ์ด ์•„๋‹Œ, ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ๋ณ„๋„์˜ ๊ฐ์ฒด์ด๋‹ค.

  • ๋”ฐ๋ผ์„œ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ํ”„๋กœํผํ‹ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด์™€ ์ƒ๊ด€ ์—†๊ณ , ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

  • ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋œ this๋„ ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

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

  • new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ์ž๋กœ์จ ํ˜ธ์ถœ๋œ๋‹ค.

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ 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 ์—ฐ์‚ฐ์ž ์—†์ด ํ˜ธ์ถœ๋˜๋ฉด ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ํ˜ธ์ถœ์ด๋‹ค
    const circle3 = Circle(15);
    console.log(circle3); //undefined
    
    //์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Circle ๋‚ด๋ถ€์˜ this๋Š” ์ „์—ญ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    console.log(radius); //15

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

apply, call,bind ๋ฉ”์„œ๋“œ๋Š” Function.prototype์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ฆ‰ ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์ด๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. apply, call

    • apply์™€ call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹๋งŒ ๋‹ค๋ฅผ ๋ฟ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

    • apply ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด ์ „๋‹ฌํ•œ๋‹ค.

    • call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.

    • ๋Œ€ํ‘œ์ ์ธ ์šฉ๋„๋Š” arguments ๊ฐ์ฒด์™€ ๊ฐ™์€ ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์— ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋‹ค.

    • arguments ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๊ธฐ๋•Œ๋ฌธ์— slice ๋ฉ”์„œ๋“œ๊ฐ™์€ ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

    function getThisBinding() {
      console.log(arguments);
      return this;
    }
    
    const thisArgs = { a: 1 };
    
    //apply ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด ํ˜•์‹
    console.log(getThisBinding.apply(thisArgs, [1, 2, 3]));
    //call ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•์‹
    console.log(getThisBinding.call(thisArgs, 1, 2, 3));
  2. bind

    • bind๋ฉ”์„œ๋“œ๋Š” apply, call ๋ฉ”์„œ๋“œ์™€ ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค๋งŒ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ’์œผ๋กœ this ๋ฐ”์ธ๋”ฉ์ด ๊ต์ฒด๋œ ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    function getThisBinding() {
      return this;
    }
    
    const thisArgs = { a: 1 };
    
    console.log(getThisBinding.bind(thisArgs)); //getThisBinding
    
    //bind ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.
    console.log(getThisBinding.bind(thisArgs)()); //{ a: 1 }
    • bind ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ์˜ this์™€ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๊ฐ€ ๋ถˆ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

    const person = {
        name : 'Lee',
        foo(callback){
            setTimeout(callback, 100);
        }
    };
    
    person.foo(function(){
        console.log(`Hi my name is ${this.name}.`); //Hi my name is .
        //๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ ์ƒ์—์„œ window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” 
    		//๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ ๊ธฐ๋ณธ ๊ฐ’์€ ''์ด๋‹ค.
    })
    const person = {
        name : 'Lee',
        foo(callback){
    				//bind ๋ฉ”์„œ๋“œ๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this ๋ฐ”์ธ๋”ฉ์„ ์ „๋‹ฌ.
            setTimeout(callback.bind(this), 100);
        }
    };
    
    person.foo(function(){
        console.log(`Hi my name is ${this.name}.`); //Hi my name is Lee.
       
    })

Last updated