Skip to content

Latest commit

ย 

History

History
442 lines (340 loc) ยท 14.9 KB

File metadata and controls

442 lines (340 loc) ยท 14.9 KB

22.1 this ํ‚ค์›Œ๋“œ

  • ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜์˜ ๋…ผ๋ฆฌ์ ์ธ ๋‹จ์œ„๋กœ ๋ฌถ์€ ๋ณตํ•ฉ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ = ๊ฐ์ฒด (์ฐธ๊ณ  19.1์ ˆ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ)
  • ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋ฉด?

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

const circle = {
  // ํ”„๋กœํผํ‹ฐ: ๊ฐ์ฒด ๊ณ ์œ ์˜ ์ƒํƒœ ๋ฐ์ดํ„ฐ
  radius: 5,
  // ๋ฉ”์„œ๋“œ: ์ƒํƒœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ๋™์ž‘
  getDiameter() {
    // ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋ฉด
    // ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์ธ circle์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
    return 2 * circle.radius;
  }
};

console.log(circle.getDiameter()); // 10
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ฐฉ์‹์œผ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ
function Circle(radius) {
  // ์ด ์‹œ์ ์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
  ????.radius = radius;
}

Circle.prototype.getDiameter = function () {
  // ์ด ์‹œ์ ์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
  return 2 * ????.radius;
};

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋จผ์ € ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.
const circle = new Circle(5);
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•œ๋‹ค. ๋‹จ, ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ์ดํ›„ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

โ†’ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋จผ์ € ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์‹œ์  = ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์ด์ „ ์ƒํƒœ, ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค. โ†’ ์ด๋ฅผ ์œ„ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” this๋ผ๋Š” ํŠน์ˆ˜ํ•œ ์‹๋ณ„์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

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

  • this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
// ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
const circle = {
  radius: 5,
  getDiameter() {
    // this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    return 2 * this.radius;
  }
};

console.log(circle.getDiameter()); // 10
  • this๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์— ์˜ํ•ด ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ, ์ฝ”๋“œ ์–ด๋””์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅ
  • this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ์˜ํ•ด ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
function Circle(radius) {
  // this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  this.radius = radius;
}

Circle.prototype.getDiameter = function () {
  // this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  return 2 * this.radius;
};

// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
const circle = new Circle(5);
console.log(circle.getDiameter()); // 10
  • this๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์ด ๋‹ค๋ฅด๋‹ค.
// this๋Š” ์–ด๋””์„œ๋“ ์ง€ ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค.
// ์ „์—ญ์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
console.log(this); // window

function square(number) {
  // ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  console.log(this); // window
  return number * number;
}
square(2);

const person = {
  name: 'Lee',
  getName() {
    // ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    console.log(this); // {name: "Lee", getName: ฦ’}
    return this.name;
  }
};
console.log(person.getName()); // Lee

function Person(name) {
  this.name = name;
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  console.log(this); // Person {name: "Lee"}
}

const me = new Person('Lee');
  • this๋Š” ์ฝ”๋“œ ์–ด๋””์—์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅ
  • ์ผ๋ฐ˜์ ์œผ๋กœ this๋Š” ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€ ๋˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. โ†’ strict mode๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ โ†’ why?
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ

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

// this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.
const foo = function () {
  console.dir(this);
};

// ๋™์ผํ•œ ํ•จ์ˆ˜๋„ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

// 1. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
foo(); // window

// 2. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜๋ฅผ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ํ• ๋‹นํ•˜์—ฌ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด obj๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
const obj = { foo };
obj.foo(); // obj

// 3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
new foo(); // foo {}

// 4. Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ
// foo ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ธ์ˆ˜์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค.
const bar = { name: 'bar' };

foo.call(bar);   // bar
foo.apply(bar);  // bar
foo.bind(bar)(); // bar
  • this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹, ์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋™์  ๊ฒฐ์ •

  • ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹

    1. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ
    2. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
    4. Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ

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

function foo() {
  console.log("foo's this: ", this);  // window
  function bar() {
    console.log("bar's this: ", this); // window
  }
  bar();
}
foo();
  • ๊ธฐ๋ณธ์ ์œผ๋กœ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด(window)๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  • ์ค‘์ฒฉ ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•˜๋ฉด ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜์— this๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค
function foo() {
  'use strict';

  console.log("foo's this: ", this);  // undefined
  function bar() {
    console.log("bar's this: ", this); // undefined
  }
  bar();
}
foo();
  • strict mode๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
// var ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ์ „์—ญ ๋ณ€์ˆ˜ value๋Š” ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‹ค.
var value = 1;
// const ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ์ „์—ญ ๋ณ€์ˆ˜ value๋Š” ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.
// const value = 1;

const obj = {
  value: 100,
  foo() {
    console.log("foo's this: ", this);  // {value: 100, foo: ฦ’}
    console.log("foo's this.value: ", this.value); // 100

    // ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜
    function bar() {
      console.log("bar's this: ", this); // window
      console.log("bar's this.value: ", this.value); // 1
    }

    // ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜๋„ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
    bar();
  }
};

obj.foo();
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ๋‹ค๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋„ ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ ๋ชจ๋“  ํ•จ์ˆ˜(์ค‘์ฒฉ ํ•จ์ˆ˜, ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํฌํ•จ) ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  • โš ๏ธ ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ฒƒ์€ ๋ฌธ์ œ!

โ†’ ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜๋ฅผ ๋•๋Š” ํ—ฌํผ ํ•จ์ˆ˜์˜ ์—ญํ• 

โ†’ this๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์€ ํ—ฌํผ ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•˜๊ธฐ ์–ด๋ ค์›€

var value = 1;

const obj = {
  value: 100,
  foo() {
    // this ๋ฐ”์ธ๋”ฉ(obj)์„ ๋ณ€์ˆ˜ that์— ํ• ๋‹นํ•œ๋‹ค.
    const that = this;

    // ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this ๋Œ€์‹  that์„ ์ฐธ์กฐํ•œ๋‹ค.
    setTimeout(function () {
      console.log(that.value); // 100
    }, 100);
  }
};

obj.foo();
  • **that**์€ ์ฃผ๋กœ JavaScript์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด๋กœ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ **this**๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋ฐ”์ธ๋”ฉ๋˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
  • ๊ทธ ์™ธ์—๋„ Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต

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

  • ์ฃผ์˜ํ•  ์ , ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์†Œ์œ ํ•œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค๋Š” ๊ฒƒ
const person = {
  name: 'Lee',
  getName() {
    // ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
    return this.name;
  }
};

// ๋ฉ”์„œ๋“œ getName์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” person์ด๋‹ค.
console.log(person.getName()); // Lee

---

const anotherPerson = {
  name: 'Kim'
};

// getName ๋ฉ”์„œ๋“œ๋ฅผ anotherPerson ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ• ๋‹น
anotherPerson.getName = person.getName;

// getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” anotherPerson์ด๋‹ค.
console.log(anotherPerson.getName()); // Kim

// getName ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹น
const getName = person.getName;

// getName ๋ฉ”์„œ๋“œ๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ
console.log(getName()); // ''
// ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ getName ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this.name์€ ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ window.name๊ณผ ๊ฐ™๋‹ค.
// ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ’์€ ''์ด๋‹ค.
// Node.js ํ™˜๊ฒฝ์—์„œ this.name์€ undefined๋‹ค.
  • ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํผํ‹ฐ์— ๋ฐ”์ธ๋”ฉ๋œ ํ•จ์ˆ˜
  • ์ฆ‰, person ๊ฐ์ฒด์˜ getName ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” person ๊ฐ์ฒด์— ํฌํ•จ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ๋ณ„๋„์˜ ๊ฐ์ฒด์ด๋‹ค.
  • ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ํ”„๋กœํผํ‹ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์™€๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
function Person(name) {
  this.name = name;
}

Person.prototype.getName = function () {
  return this.name;
};

const me = new Person('Lee');

// getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” me๋‹ค.
console.log(me.getName()); // โ‘  Lee

Person.prototype.name = 'Kim';

// getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” Person.prototype์ด๋‹ค.
console.log(Person.prototype.getName()); // โ‘ก Kim
  • ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋œ this๋„ ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. โ‘  getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” me โ†’ this๋Š” me

โ‘ก getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” Person.prototype โ†’ this๋Š” Person.prototype

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

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ (๋ฏธ๋ž˜์—) ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
function Circle(radius) {
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius;
  };
}

// ๋ฐ˜์ง€๋ฆ„์ด 5์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const circle1 = new Circle(5);
// ๋ฐ˜์ง€๋ฆ„์ด 10์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const circle2 = new Circle(10);

console.log(circle1.getDiameter()); // 10
console.log(circle2.getDiameter()); // 20

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

  • Function.prototype.apply / Function.prototype.call ๋ฉ”์„œ๋“œ๋Š” this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด์™€ ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ

[apply] ์ž‘์„ฑ๋ฒ• : fn.apply(thisArg, [argsArray]) this ์ธ์ž๋ฅผ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๊ณ , ๋‘๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” ๋ฐฐ์—ด์„ ๋ฐ›์Œ [call] ์ž‘์„ฑ๋ฒ• : fn.call(thisArg[, arg1[, arg2[, ...]]]) this ์ธ์ž๋ฅผ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๊ณ , ๋‘๋ฒˆ์งธ ์ธ์ž๋ถ€ํ„ฐ๋Š” ๋ฐฐ์—ด์ด ์•„๋‹Œ ๊ฐ ์ธ์ž๋กœ ๋ฐ›์Œ

  • ๋‘ ๋ฉ”์„œ๋“œ์˜ ๋ณธ์งˆ์ ์ธ ๊ธฐ๋Šฅ์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ
function getThisBinding() {
  return this;
}

// this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
const thisArg = { a: 1 };

console.log(getThisBinding()); // window

// getThisBinding ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด๋ฅผ getThisBinding ํ•จ์ˆ˜์˜ this์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
console.log(getThisBinding.apply(thisArg)); // {a: 1}
console.log(getThisBinding.call(thisArg)); // {a: 1}
  • getThisBining ํ•จ์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š” ์˜ˆ์ œ
function getThisBinding() {
  console.log(arguments);
  return this;
}

// this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
const thisArg = { a: 1 };

// getThisBinding ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด๋ฅผ getThisBinding ํ•จ์ˆ˜์˜ this์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
// apply ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด ์ „๋‹ฌํ•œ๋‹ค.
console.log(getThisBinding.apply(thisArg, [1, 2, 3]));
// Arguments(3) [1, 2, 3, callee: ฦ’, Symbol(Symbol.iterator): ฦ’]
// {a: 1}

// call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.
console.log(getThisBinding.call(thisArg, 1, 2, 3));
// Arguments(3) [1, 2, 3, callee: ฦ’, Symbol(Symbol.iterator): ฦ’]
// {a: 1}
  • apply ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด ์ „๋‹ฌ
  • call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ
function convertArgsToArray() {
  console.log(arguments);

  // 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๊ฐ€ ๋ถˆ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ
function getThisBinding() {
  return this;
}

// this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
const thisArg = { a: 1 };

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

์ •๋ฆฌ

ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹ this ๋ฐ”์ธ๋”ฉ
์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „์—ญ ๊ฐ์ฒด
๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด
์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ (๋ฏธ๋ž˜์—) ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค
Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด