this

์ •์˜

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ this๋ž€ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๋‹ค. ๋ผ๊ณ  ์“ฐ์—ฌ์žˆ๋‹ค. ๋ง์ด ์ข€ ์–ด๋ ค์šด๋ฐ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด์ž.

let person = {
  firstName: "Kim",
  lastName: "Chungyeon",
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.fullName()); //Kim ChungYeon

์ด ์˜ˆ์‹œ ์ฝ”๋“œ๋Š” this ์˜ ์ •์˜ ์ค‘ "์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”~" ์— ๋Œ€ํ•œ ์ฝ”๋“œ์ด๋‹ค. ์˜ˆ์‹œ์—์„œ this๋Š”person ๊ฐ์ฒด์˜ fullName ๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. this๋Š” ์‹คํ–‰ ์ค‘์ธ ํ•จ์ˆ˜๊ฐ€ ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ this๋Š” ๋ฐ”๋กœ person ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

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

Person.prototype.introduce = function() {
  console.log("์•ˆ๋…•ํ•˜์„ธ์š”, ์ œ ์ด๋ฆ„์€ " + this.name + "์ž…๋‹ˆ๋‹ค.");
}

// ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
let person1 = new Person("Kim");
let person2 = new Person("Lee");

person1.introduce(); // ์•ˆ๋…•ํ•˜์„ธ์š”, ์ œ ์ด๋ฆ„์€ Kim์ž…๋‹ˆ๋‹ค.
person2.introduce(); // 

์ด๋ฒˆ์—๋Š” "์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”~"์— ๋Œ€ํ•œ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

์˜ˆ์‹œ์—์„œ this๋Š” Person ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…์— ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ์ธ introduce์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. ์ฆ‰, ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” "์ž์‹ "์€ ํ”„๋กœํ† ํƒ€์ž…์„ ๋งŒ๋“  Person ๊ฐ์ฒด์ด๋ฉฐ, Person ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”๋กœ person1๊ณผ person2 ์ด๋‹ค. ๋”ฐ๋ผ์„œ this๋Š” Person์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์ธ person1๊ณผ person2๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.

๋‘ ๋ฒˆ์งธ ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ ์•Œ์•„๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ, this๋Š” ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์ƒ์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์ด ๊ฒฐ์ •๋˜๋ฉฐ, this์˜ ๊ฐ’์ด ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์„ ๋ฐ”์ธ๋”ฉ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋ฐ”์ธ๋”ฉ์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ์‚ดํŽด๋ณด์ž

๋ฐ”์ธ๋”ฉ

์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด์ธ window์— ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. (node.js์—์„œ๋Š” global)

console.log(this) // window

function test() {
  console.log(this); // Window {0: Window, ...}
  function inner() {
    console.log(this); // Window {0: Window, ...}
  }
  inner();
}
test();

๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ์—๋Š” ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์‹œ ์ฝ”๋“œ 1 ์—์„œ ํ™•์ธ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

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

์•„๋ž˜์—์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด์ž.

const obj = {
	normal: function() {
		console.log('normal fn this:', this);
	},
	arrow: () => {
		console.log('arrow fn this:', this);
	},
}

obj.normal(); // normal fn this: obj
obj.arrow(); // arrow fn this: window

๋‹ค์Œ์œผ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง€๋Š” ์ธ์Šคํ„ด์Šค์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

function User(name) {
  this.name = name;
}
const user1 = new User('Kim');
console.log(user1.name); // Kim

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ, ์ „์—ญ๊ฐ์ฒด๋กœ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

์œ„์—์„œ ์ž ์‹œ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ, ํ™”์‚ดํ‘œํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉ๋œ this์˜ ๊ฒฝ์šฐ, Lexical this๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„์— ์กด์žฌํ•˜๋Š” this๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๊ฒŒ ๋œ๋‹ค.

const arrow= () => {
	console.log(this);
}

arrow(); // window

// arrow ๋ฉ”์„œ๋“œ์˜ ์Šค์ฝ”ํ”„์— ์ •์˜๋˜์žˆ์œผ๋ฏ€๋กœ, this๊ฐ€ arrow ๋ฉ”์„œ๋“œ์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
const obj = {
	arrow: function() {
		const arrowInner = () => {
			console.log(this);
		}

		arrowInner ();
	}
};

obj.arrow(); // obj

์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด obj ๊ฐ์ฒด์˜ arrow ๋ฉ”์„œ๋“œ์˜ ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„๋Š” ์ƒ์„ฑ๋˜์—ˆ์„ ๋‹น์‹œ์˜ ํ™˜๊ฒฝ์ธ obj ๊ฐ์ฒด์ด๊ณ , arrowInner ๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์ด๋ฏ€๋กœ, this๋Š” arrow ๋ฉ”์„œ๋“œ์˜ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ arrow ๋ฉ”์„œ๋“œ์˜ ์Šค์ฝ”ํ”„์˜ this๊ฐ€ ๋ฐ”๋กœ obj ๊ฐ์ฒด์ด๋ฏ€๋กœ this๋Š” obj ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์—์„œ this๋ฅผ ์‚ดํŽด๋ณด์ž.

const button = document.getElementById('button');
const consoleThis = function (e) {
  console.log(e.target === this, this);
};

button.addEventListener('click', consoleThis);
// <button id="button">click</button>

์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ์—์„œ this๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์€ HTML ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์˜ˆ์‹œ ์ฝ”๋“œ์—์„œ๋Š” button ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์คฌ๊ธฐ ๋•Œ๋ฌธ์—, console.log ์— ์ถœ๋ ฅ ๋˜๋Š” ๊ฐ’, this๋Š” ๋ฐ”๋กœ button ์š”์†Œ ์ž์ฒด์ด๋‹ค.

Last updated