Javscript ์—์„œ์˜ this๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋ฐ”์ธ๋”ฉํ•  ๊ฐ์ฒด๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.

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

ํ•จ์ˆ˜ ํ˜ธ์ถœ

์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ „์—ญ ๊ฐ์ฒด๋กœ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰ ์‹œ ๋‹ค์Œ ํ•จ์ˆ˜ ์•ˆ์˜ this๋Š” window์™€ ์—ฐ๊ฒฐ๋œ๋‹ค. (strict ๋ชจ๋“œ์—์„œ๋Š” undefined). ์ด๋Š” ๋ฌผ๋ก  ๋‚ด๋ถ€ ํ•จ์ˆ˜๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค

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

์ด๋Š” ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ์•ˆ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜, ์ฝœ๋ฐฑ์ผ ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

var obj = {
  value: 100,
  foo: function() {
    console.log("foo's this: ",  this);  // obj
    function bar() { /* ๋‚ด๋ถ€ํ•จ์ˆ˜ */
      console.log("bar's this: ",  this); // window
    }
    bar();
 
    setTimeout(function() {  /* ์ฝœ๋ฐฑํ•จ์ˆ˜ */
      console.log("callback's this: ",  this);  // window
    }, 100);
  }
};
 
obj.foo();

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

์ด๋ฅผ ํšŒํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„  ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

  • var innerThis = this; ์™€ ๊ฐ™์ด ํ˜„์žฌ ๊ฐ์ฒด์˜ this๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•˜์—ฌ ์‚ฌ์šฉ
  • call, bind, apply๋กœ this ์„ค์ •
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ

๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ทธ ๊ฐ์ฒด์™€ ์—ฐ๊ฒฐ์ด ๋œ๋‹ค.

var obj = {
	a : function {console.log(this)}
}
obj.a(); // obj

ํ•˜์ง€๋งŒ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์„œ๋“œ๋ฅผ ๊บผ๋‚ด์˜จ๋‹ค๋ฉด ๋ฐ”์ธ๋”ฉ ๋˜๋Š” this๋Š” ๋‹ฌ๋ผ์ง„๋‹ค

var a2 = obj.a;
a2() // Window {}

this๋Š” ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜์— ๋”ฐ๋ผ ๋ฐ”์ธ๋”ฉ ๋˜๊ธฐ์—, a2๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ a2๋Š” ์ „์—ญ ๊ฐ์ฒด์— ํฌํ•จ์ด ๋˜๊ธฐ์— ์ „์—ญ ๊ฐ์ฒด์™€ ์—ฐ๋™์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

ํ”„๋กœํ† ํƒ€์ž…

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

function Person(name) {
  this.name = name;
}
Person.prototype.getName = function() {
  return this.name;
}
var me = new Person('Lee');
console.log(me.getName());   // "Lee"
 
Person.prototype.name = 'Kim';
console.log(Person.prototype.getName());  // "Kim"

์ฝœ๋ฐฑํ•จ์ˆ˜ ํ˜ธ์ถœ

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ์ „์—ญ ๊ฐ์ฒด๋กœ ์—ฐ๋™ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์œ„์—์„œ ์‚ดํŽด๋ดค๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ์˜ˆ์‹œ๋„ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ „์—ญ ๊ฐ์ฒด๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์ฒซ๋ฒˆ์งธ ์ฝ˜์†” ๊ฒฐ๊ณผ๊ฐ€ Not Set์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

let userData = {
    signUp: '2020-10-06 15:00:00',
    id: 'minidoo',
    name: 'Not Set',
    setName: function(firstName, lastName) {
        this.name = firstName + ' ' + lastName;
    }
}
 
function getUserName(firstName, lastName, callback) {
    callback(firstName, lastName);
}
 
getUserName('PARK', 'MINIDDO', userData.setName);
 
console.log('1: ', userData.name); // Not Set
console.log('2: ', window.name); // PARK MINIDDO

์ด๋•Œ๋Š” call, apply, bind๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค

function getUserName(firstName, lastName, callback) {
    callback.call(userData, firstName, lastName);
}
 
getUserName('PARK', 'MINIDDO', userData.setName);
 
console.log('1: ', userData.name); // PARK MINIDDO
console.log('2: ', window.name); // undefined

apply/call/bind ํ˜ธ์ถœ

func.apply(thisArg, [argsArray])
func.call(thisArg, argsArray)
func.bind(thisArg)(argsArray)
 
// thisArg: ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์— ๋ฐ”์ธ๋”ฉํ•  ๊ฐ์ฒด
// argsArray: ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  argument์˜ ๋ฐฐ์—ด

ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜์— ๋ฐ”์ธ๋”ฉํ•  this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์—์„œ์˜ this

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์—์„œ๋Š” ๊ทธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋ถ€์ฐฉ๋œ dom์ด this๋กœ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

document.body.onclick = function() {
  console.log(this); // <body>
}

ํ•˜์ง€๋งŒ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‚ด๋ถ€ ํ•จ์ˆ˜๋Š” ์ „์—ญ ๊ฐ์ฒด๋กœ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

document.body.onclick = function() {
  console.log(this); // body
  function inner() {
    console.log('inner', this); // inner Window
  }
  inner();
}

์ด ๋ฌธ์ œ๋Š” ์œ„์—์„œ ์‚ดํŽด๋ณด์•˜๋“ฏ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค

  • var innerThis = this; ์™€ ๊ฐ™์ด ํ˜„์žฌ ๊ฐ์ฒด์˜ this๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•˜์—ฌ ์‚ฌ์šฉ
  • call, bind, apply๋กœ this ์„ค์ •
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

์ฒด์ด๋‹

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

let ladder = {
  step: 0,
  up() {
    this.step++;
    return this;
  },
  down() {
    this.step--;
    return this;
  },
}
 
ladder.up().down().up().down(); 

ํ™”์‚ดํ‘œํ•จ์ˆ˜์˜ this

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

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

ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ํŠน์„ฑ ๋•Œ๋ฌธ์— ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ณณ๋“ค์ด ์žˆ๋‹ค

  • ๋ฉ”์†Œ๋“œ : ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์–ด ํ˜„์žฌ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋จ.
const obj = {
	name : "name",
	foo: () => console.log(this.name)  // undefined. 
}
  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ : ํ™”์‚ดํ‘œํ•จ์ˆ˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์“ธ ์ˆ˜ ์—†์Œ
const Foo = () => {}
const foo = new Foo() // TypeError: Foo is not a constructor
  • ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ : ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ์˜ this๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ถ€์ฐฉ๋œ ๊ณณ์„ ์˜๋„ํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๋ฐ ํ™”์‚ดํ‘œํ•จ์ˆ˜ ์‚ฌ์šฉ์‹œ ์ „์—ญ๊ฐ์ฒด๊ฐ€ ๋ถ™์–ด๋ฒ„๋ฆฐ๋‹ค.
button.addEventListener('click', () => {
  console.log(this);	// Window
  this.innerHTML = 'clicked';
});
document.body.onclick = () => {
  console.log(this); // Window
}

์ฐธ๊ณ