๐Ÿ“‹ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive | 22์žฅ this

waterglassesยท2022๋…„ 5์›” 14์ผ
0
post-thumbnail

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive ๋„์„œ์˜ 22์žฅ์„ ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

22.1 this ํ‚ค์›Œ๋“œ

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

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

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

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

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

// 22-03
const circle = {
  radius: 5,
  getDiameter() {
    // this๋Š” circle์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  	return 2 * this.radius;
  }
};
// 22-05
console.log(this); //window

function square(number) {
  console.log(this); // window
  return number * number;
}
square(2);

const person = {
  name: 'waterglasses',
  getName() {
    // ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this: ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด
    console.log(this); // {name: 'waterglasses", getName: f} 
    return this.name;
  }
}
console.log(person.getName()); // waterglasses

'use strict'
function Person(name) {
  this.name = name;
  console.log(this);
}

function Person(name) {
  this.name = name;
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this: ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค
  console.log(this); // Person {name: "waterglasses"}
}
const me = new Person('waterglasses');

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

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

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

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

// 22-07
function foo() {
  console.log("foo's this: ", this); // window
}
foo();
  • ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ => ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.(์ „์—ญ, ์ค‘์ฒฉํ•จ์ˆ˜, ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜, ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋ชจ๋‘)
// 22-11
var value = 1;

const obj = {
  value: 100,
  foo() {
    const that = this; // this๋ฅผ that์— ํ• ๋‹น
    
    setTimeout(function () {
      console.log(that.value);
    }, 100);
    // ํ˜น์€
    setTimeout(() => console.log(this.valueu), 100); // 100
  }
};

obj.foo();

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

๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this์—๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด, ์ฆ‰ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์•ž์— ๋งˆ์นจํ‘œ(.) ์—ฐ์‚ฐ์ž ์•ž์— ๊ธฐ์ˆ ํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

// 22-16
function Person(name) {
  this.name = name;
}

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

const me = new Person('Lee');

// me๊ฐ€ ํ˜ธ์ถœ
console.log(me.getName()); // Lee

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

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

// 22-18
const circle3 = Circle(15);
console.log(circle3); //undefined
console.log(radius); //15

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

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

// 22-19
function getThisBinding() {
  return this;
}
const thisArgs = { a: 1 };

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

console.log(getThisBinding.bind(thisArg)); // getThisBinding
console.log(getThisbinding.bind(thisArg)()); // {a: 1}
  • apply์™€ call๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ํŠน์ • ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์˜ this์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
  • bind๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์ง€๋งŒ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ’์œผ๋กœ this๋ฐ”์ธ๋”ฉ์ด ๊ต์ฒด๋œ ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// 22-23
const person = {
  name: 'Lee',
  foo(callback) {
    console.log(this); // {name: 'Lee', foo: ฦ’}
  	setTimeout(callback, 100);
    // bind๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ
    setTimeout(callback.bind(this), 100);
  }
}

person.foo(function () {
  console.log(this.name); // window์—์„œ๋Š” '', Node.js์—์„œ๋Š” undefined
})

Ref

  • ์ด์›…๋ชจ ์ €, โŒœ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep DiveโŒŸ, ์œ„ํ‚ค๋ถ์Šค
profile
๋งค ์ˆœ๊ฐ„ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

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