본문 바로가기

Javascript/모던 자바스크립트 DeepDive

[모던 자바스크립트 Deep Dive] 22 this

this 키워드

  • this는 자신이 속한 객체 또는 자신이 생성할 인스턴스를 가리키는 자기 참조 변수 self-referencing variable
  • this를 통해 자신이 속한 객체 또는 자신이 생성할 인스턴스의 프로퍼티나 메서드를 참조할 수 있음
  • this를 가리키는 값, this 바인딩은 함수 호출 방식에 의해 동적으로 결정 / 클래스 기반 언어인 자바나 c++에서는 언제나 클래스가 생성하는 인스턴스를 가리킴
  • this는 객체의 프로퍼티나 메서드를 참조하기 위한 자기 참조변수이므로 일반 함수에서는 의미가 없음. strict mode가 적용된 일반 함수의 내부의 this에는 undefined가 바인딩
// this는 어디서든지 참조 가능
// 전역에서 this는 전역 객체 window를 가리킴
console.log(this); // Window

function square(number) {
  // 일반 함수 내부에서 this는 전역 객체 window를 가리킴
  console.log(this); // Window
  return number * number;
}
console.log(square(2)); // 4

function strictFn() {
  "use strict"; // srict mode 적용된 일반 함수 내부의 this는 undefined가 바인딩
  console.log(this); // undefined
}
strictFn();

const person = {
  name: "Jin",
  getName() {
    // 메서드 내부에서 this는 메서드를 호출한 객체를 가리킴
    console.log(this); // {name: 'Jin', getName: ƒ}
    return this.name;
  },
};
console.log(person.getName()); //Jin

function Person(name) {
  this.name = name;
  // 생성자 함수 내부에서 this는 생성자 함수가 생성할 인스턴스를 가리킴
  console.log(this); // Person {name: 'Jin'}
}

const me = new Person("Jin");

함수 호출 방식과 this 바인딩

  • this 바인딩(this에 바인딩될 값)은 함수 호출 방식, 즉 함수가 어떻게 호출되었는가에 따라 동적으로 결정
  • 비교: 함수의 상위 스코프를 결정하는 방식인 렉시컬 스코프는 함수 정의가 평가되어 함수 객체가 생성되는 시점에 상위 스코프 결정
  • 함수 호출하는 방식
    • 일반 함수 호출
    • 메서드 호출
    • 생성자 함수 호출
    • Function.prototype.apply/call/bind 메서드에 의한 간접 호출
// this 바인딩은 함수 호출 방식에 따라 동적으로 결정
const foo = function () {
  console.dir(this);
};

// 일반 함수 호출
// foo 함수 내부의 this는 전역 객체 window를 가리킴
foo(); // Window

// 메서드 호출
// foo 함수 내부의 this는 호출한 객체 obj를 가리킴
const obj = { foo };
obj.foo(); // obj

// 생성자 함수 호출
// foo 함수 내부의 this는 생성자 함수가 생성한 인스턴스를 가리킴
new foo(); // foo

// Function.prototype.apply/call/bind 메서드에 의한 간접 호출
// foo 함수 내부의 this는 인수에 의해 결정
const bar = { name: "bar" };
foo.call(bar); // bar
foo.apply(bar); // bar
foo.bind(bar)(); // bar

일반 함수 호출

  • 일반 함수로 호출된 모든 함수(중첩 함수, 콜백 함수 포함) 내부의 this에는 전역 객체가 바인딩
    • this 대신 콜백함수 내에서 this를 할당한 변수 사용
    • Function.prototype.apply/call/bind 메서드를 사용하여 명시적으로 this 바인딩
    • 화살표 함수 (권장)
  • strict mode가 적용된 일반 함수의 내부에는 undefined 바인딩
var value = 1;

const obj = {
  value: 100,
  foo() {
    console.log("foo's this: ", this); // foo's this:  {value: 100, foo: ƒ}
    setTimeout(function () {
      console.log("callback's this: ", this); // callback's this:  Window
      console.log(this.value); // 1
    }, 100);
  },
};

obj.foo();
// this 대신 콜백함수 내에서 this를 할당한 변수 사용
var value = 1;

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

obj.foo();
// Function.prototype.apply/call/bind 메서드를 사용하여 명시적으로 this 바인딩
var value = 1;

const obj = {
  value: 100,
  foo() {
    console.log("foo's this: ", this); // foo's this:  {value: 100, foo: ƒ}
    setTimeout(
      function () {
        console.log("callback's this: ", this); // callback's this:  {value: 100, foo: ƒ}
        console.log(this.value); // 100
      }.bind(this),
      100
    );
  },
};

obj.foo();
// 화살표 함수

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

obj.foo();

메서드 호출

  • 메서드 내부의 this는 메서드를 소유한 객체가 아닌 메서드를 호출한 객체에 바인딩 됨
function Person(name) {
  this.name = name;
}

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

const me = new Person("Jin");

// getName 메서드를 호출한 객체는 me
console.log(me.getName()); // Jin

Person.prototype.name = "Park";

// getName 메서드를 호출한 객체는 Person.prototype
console.log(Person.prototype.getName()); // Park

프로토타입 메서드와 this 바인딩

생성자 함수 호출

  • 생성자 함수 내부의 this에는 생성자 함수가 (미래에) 생성할 인스턴스가 바인딩
  • new 연산자와 함께 호출하지 않으면 생성자 함수가 아닌 일반 함수의 호출임
// 생성자 함수
function Circle(radius) {
  // 생성자 함수 내부의 this는 생성자 함수가 생성할 인스턴스를 가리킴
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius;
  };
}

const circle1 = new Circle(5);
const circle2 = new Circle(10);
// new 연산자와 함께 호출하지 않으면 생성자 함수로 동작 x, 일반 함수 호출임
const circle3 = Circle(15);

console.log(circle1.getDiameter()); // 10
console.log(circle2.getDiameter()); // 20
// 일반 함수로 호출된 Circle 내부의 this는 전역 객체를 가리킴
console.log(radius); // 15

Function.prototype.apply/call/bind 메서드에 의한 간접 호출

  • apply, call, bind 메서드는 Function.prototype의 메서드이므로 모든 함수가 상속 받아 사용 가능
  • apply와 call의 본질적인 기능은 함수를 호출하는 것
    • apply와 call은 호출 함수에 인수를 전달하는 방식만 다를 뿐 동일하게 동작
      • apply 메서드: 호출할 함수의 인수를 배열로 묶어 전달
      • call 메서드: 호출할 함수의 인수를 쉼표로 구분한 리스트 형식으로 전달
    • 대표적인 용도는 arguments 같은 유사 배열 객체에 배열 메서드를 사용하는 경우
  • bind는 첫 번째 인수로 전달한 값으로 this 바인딩이 교체된 함수를 새롭게 생성해 반환. apply와 call과 달리 함수를 호출하지 않기 때문에 명시적으로 호출해줌
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}
function convertArgsToArray() {
  console.log(arguments); // [Arguments] { '0': 1, '1': 2, '2': 3 }

  // arguments 객체를 배열로 변환
  // Array.prototype.slice를 인수 없이 호출하면 배열의 복사본 생성
  const arr = Array.prototype.slice.apply(arguments);
  console.log(arr); // [ 1, 2, 3 ]
  return arr;
}

convertArgsToArray(1, 2, 3); // [ 1, 2, 3 ]
function getThisBinding() {
  return this;
}

// this로 사용할 객체
const thisArg = { a: 1 };

// bind 메서드는 첫 번째 인수로 전달한 thisArg로 this바인딩이 교체된 함수를 새롭게 생성해 반환
console.log(getThisBinding.bind(thisArg)); // [Function: bound getThisBinding]
// bind 메서드는 함수를 호출하지는 않으므로 명시적으로 호출해야 함
console.log(getThisBinding.bind(thisArg)()); // { a: 1 }
// 메서드 내부의 중첩 함수 또는 콜백 함수의 this가 불일치하는 문제
const person = {
  name: "Jin",
  foo(callback) {
    setTimeout(callback, 100);
  },
};

person.foo(function () {
  console.log(`Hi! my name is ${this.name}.`); // Hi! my name is . ; 브라우저 환경의 window.name은 ""
});
// bind 메서드는 this와 메서드 내부의 중첩 함수 또는 콜백 함수의 this가 불일치하는 문제를 해결하기 위해 사용됨
const person = {
  name: "Jin",
  foo(callback) {
    // bind 메서드로 callback 함수 내부의 this 바인딩을 전달
    setTimeout(callback.bind(this), 100);
  },
};

person.foo(function () {
  console.log(`Hi! my name is ${this.name}.`); // Hi! my name is Jin.
});
함수 호출 방식 this 바인딩
일반 함수 호출  전역 객체
메서드 호출 메서드를 호출한 객체
생성자 함수 호출 생성자 함수가 (미래에) 생성할 인스턴스
Function.prototype.apply/call/bind 메서드에 의한 간접 호출 메서드에 첫 번째 인수로 전달한 객체