66651
๐Ÿ‘€
66651
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (57)
    • note (28)
    • log (13)
    • error (9)
    • etc (7)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
66651

๐Ÿ‘€

note

[Deep Dive] 22์žฅ this

this ํ‚ค์›Œ๋“œ

this๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜๋‹ค. this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ  arguments ๊ฐ์ฒด์™€ this๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ „๋‹ฌ๋œ๋‹ค. ๋‹จ, this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’, ์ฆ‰ this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ์˜ํ•ด ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.

 

// ์ „์—ญ์—์„œ this → ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
console.log(this); //window

// ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this → ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
function test() {
  console.log(this); // window
}

// stric mode๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this → undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
// (์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ)
function stric() {
  'use strict';
  console.log(this); // undefined
}

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

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

 

 

 

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

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

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

function one() {
  console.log(this); // window
  function two() {
    console.log(this); // window
  }
  two();
}
one();

const obj = {
  value: 100,
  one() {
    console.log(this); // {value: 100, one: f}

    function two() {
      console.log(this); // window
    }
    two();

    // ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ๋‹ค๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€ this์—๋„ ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ
    setTimeout(function () {
      console.log(this); // window
    }, 0);
  },
};
obj.one();

 

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

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

    setTimeout(function () {
      console.log(that); // {value: 100, one: f}
    }, 0);

    // ๋ฐฉ๋ฒ•2. ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ๋ฐ”์ธ๋”ฉ
    setTimeout(
      function () {
        console.log(this); // {value: 100, one: f}
      }.bind(this),
      0
    );

    // ๋ฐฉ๋ฒ•3. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    setTimeout(() => console.log(this), 0); // {value: 100, one: f}
  },
};
obj.one();

 

 

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

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

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

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

 

๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์— ํฌํ•จ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ๋ณ„๋„์˜ ๊ฐ์ฒด๋‹ค. ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์„ ๋ฟ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”์„œ๋“œ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ  ์ผ๋ฐ˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋  ์ˆ˜๋„ ์žˆ๋‹ค.
const person1 = {
  name: 'Lee',
  getName() {
    return this.name;
  },
};

const person2 = {
  name: 'Kim',
};

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

console.log(person2.getName()); // Kim

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

console.log(getName()); // '' (window.name ์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ)

 

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

function Person(name) {
  this.name = name;
}

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

const me = new Person('Lee');
console.log(me.getName()); // Lee

Person.prototype.name = 'Kim';
console.log(Person.prototype.getName()); // Kim

 

 

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

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

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

const person1 = new Person('Lee');
const person2 = new Person('Kim');

console.log(person1.getName()); // 'Lee'
console.log(person2.getName()); // 'Kim'

 

 

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

Function.prototype.apply(thisArg[, argArray])
  • thisArg : this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
  • argArray : ํ•จ์ˆ˜์—๊ฒŒ ์ „๋‹ฌํ•  ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฐฐ์—ด ๋˜๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด (ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด ์ „๋‹ฌ)
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ return

 

Function.prototype.call(thisArg[, arg1 [, arg2 [, ...]]])
  • thisArg : this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
  • arg1, arg2, ... : ํ•จ์ˆ˜์—๊ฒŒ ์ „๋‹ฌํ•  ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ (ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ)
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ return

 

Function.prototype.bind(thisArg[, arg1[, arg2[, ...]]])
  • thisArg : this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
  • arg1, arg2, ... : ํ•จ์ˆ˜์—๊ฒŒ ์ „๋‹ฌํ•  ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ (ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ)
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  this ๋ฐ”์ธ๋”ฉ์ด ๊ต์ฒด๋œ ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•ด์„œ retrun

 

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

function convertArgsToArray() {
  console.log(arguments);
  const arr = Array.prototype.slice.call(arguments);
  console.log(arr);

  return arr;
}

convertArgsToArray(1, 2, 3); // [1, 2, 3]

 

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

const person = {
  name: 'Lee',
  test(callback) {
    setTimeout(callback.bind(this), 100);
  },
};

person.test(function () {
  console.log(this.name); // Lee
});

 

ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹ this ๋ฐ”์ธ๋”ฉ
์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „์—ญ ๊ฐ์ฒด
๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด
์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค
Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ Function.prototype.apply/caa/bind ๋ฉ”์„œ๋“œ์— ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
    'note' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • Node.js๋กœ CRA ์‹œ ๊ธฐ๋ณธ ํŒŒ์ผ๊ณผ ํด๋” ํ•œ ๋ฒˆ์— ์„ธํŒ…ํ•˜๊ธฐ
    • [Deep Dive] 42์žฅ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
    • [Deep Dive] 16์žฅ ํ”„๋กœํผํ‹ฐ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ
    • [JavaScript] ์‰ผํ‘œ ์—ฐ์‚ฐ์ž (Comma Operator)
    66651
    66651
    always awake

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”