[JAVA SCRIPT] this (1)

์ฐจ์Šˆยท2024๋…„ 4์›” 23์ผ
0

JAVA SCRIPT

๋ชฉ๋ก ๋ณด๊ธฐ
17/24
post-thumbnail
post-custom-banner

(1) ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” this

๐Ÿ’ก ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋Š” ์‹คํ–‰ํ•  ์ฝ”๋“œ์— ์ œ๊ณตํ•  ํ™˜๊ฒฝ ์ •๋ณด๋“ค์„ ๋ชจ์•„๋†“์€ ๊ฐ์ฒด

  • ๊ทธ ๊ฐ์ฒด ์•ˆ์—๋Š” 3๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค.
    โœ“ VariableEnvironment
    โœ“ LexicalEnvironment
    โœ“ ThisBindings

1. this๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๊ฒฐ์ • = bind

this๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ฒฐ์ •๋œ๋‹ค.

1. ์ „์—ญ ๊ณต๊ฐ„์—์„œ์˜ this

- ์ „์—ญ ๊ณต๊ฐ„์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด
- ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ this๋Š” window(๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ) ๋˜๋Š” global(node ํ™˜๊ฒฝ)๋ฅผ ๊ฐ๊ฐ ๊ฐ€๋ฆฌํ‚ด -> (๊ฐœ๋ฐœ์ž๋„๊ตฌ -> ์ฝ˜์†”์—์„œ ํ™•์ธ)

// <๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ this ํ™•์ธ)
console.log(this);
console.log(window);
console.log(this === window);
// <node ํ™˜๊ฒฝ this ํ™•์ธ>
 console.log(this);
 console.log(global);
 console.log(this === global);

2. ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœํ•  ๋•Œ ๊ทธ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ์˜ this

2-1 ํ•จ์ˆ˜ vs ๋งค์„œ๋“œ

ํ•จ์ˆ˜ ๋Š” ๊ทธ ์ž์ฒด๋กœ ๋…๋ฆฝ์ ์ธ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰ -> ์ „์—ญ๊ฐ์ฒด

ํ•จ์ˆ˜๋ช…();

๋งค์„œ๋“œ๋Š” ์ž์‹ ์„ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ -> ํ˜ธ์ถœ์˜ ์ฃผ์ฒด๊ฐ€ ์žˆ์Œ

๊ฐ์ฒด. ๋ฉ”์„œ๋“œ๋ช…()

2-2 this์˜ ํ• ๋‹น

// CASE1 : ํ•จ์ˆ˜
// ํ˜ธ์ถœ ์ฃผ์ฒด๋ฅผ ๋ช…์‹œํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ
var func = function (x) {
	console.log(this, x);
};
func(1); // Window { ... } 1
//obj1.func() ์ด๋Ÿฐ์‹์œผ๋กœ ํ˜ธ์ถœ์„ ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— window ๊ฐ์ฒด ์ผ ์ˆ˜ ๋ฐ–์— ์—†์Œ 

----------------------------------------------------------------------

// CASE2 : ๋ฉ”์„œ๋“œ
// ํ˜ธ์ถœ ์ฃผ์ฒด๋ฅผ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— this๋Š” ํ•ด๋‹น ๊ฐ์ฒด(obj)๋ฅผ ์˜๋ฏธ
// obj๋Š” ๊ณง { method: f }
var obj = {
	method: func,
};
obj.method(2); // { method: f } 2

2-3 ํ˜ธ์ถœ ๊ตฌ๋ถ„ ๊ธฐ์ค€

์ (.) , ๋Œ€๊ด„ํ˜ธ ([])

  • ์–ด๋–ค ๊ฑธ ํ˜ธ์ถœํ•˜๋“  ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ
var obj = {
	method: function (x) { console.log(this, x) }
};
obj.method(1); // { method: f } 1
obj['method'](2); // { method: f } 2

2-4 ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ์˜ this

this๋Š” ํ˜ธ์ถœ์„ ๋ˆ„๊ฐ€ ํ–ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด๋‹ค

var obj = {
	methodA: function () { console.log(this) },
	inner: {
		methodB: function() { console.log(this) },
	}
};

obj.methodA();             // this === obj
obj['methodA']();          // this === obj

obj.inner.methodB();       // this === obj.inner
obj.inner['methodB']();    // this === obj.inner
obj['inner'].methodB();    // this === obj.inner
obj['inner']['methodB'](); // this === obj.inner

3. ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•  ๋•Œ (= ํ˜ธ์ถœ ์ฃผ์ฒด๋ฅผ ์•Œ ์ˆ˜ ์—†์Œ) ๊ทธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

3-1. ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

  • ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ this๋Š” ์ง€์ •๋˜์ง€ ์•Š์Œ -> ํ˜ธ์ถœ ์ฃผ์ฒด๋ฅผ ์•Œ ์ˆ˜ ์—†์–ด์„œ
  • ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•  ๋‹น์‹œ this๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, this = ์ „์—ญ๊ฐ์ฒด
  • ํ•จ์ˆ˜๋กœ์„œ '๋…๋ฆฝ์ '์œผ๋กœ ํ˜ธ์ถœ -> this๋Š” ํ•ญ์ƒ ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค

3-2. ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ํ•จ์ˆ˜์—์„œ์˜ this

  • ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€๋ผ๊ณ  ํ•ด๋„ ํ•จ์ˆ˜๋กœ์„œ์˜ ํ˜ธ์ถœ์ด๋ฉด ์ „์—ญ๊ฐ์ฒด
var obj1 = {
	outer: function() {
		console.log(this); // (1) obj1
		var innerFunc = function() {
			console.log(this); // (2) ์ „์—ญ๊ฐ์ฒด, (3) obj2
		}
		innerFunc();

		var obj2 = {
			innerMethod: innerFunc
		};
		obj2.innerMethod();
	}
};
obj1.outer();

this ๋ฐ”์ธ๋”ฉ์— ๊ด€ํ•ด์„œ๋Š” ์˜ค์ง ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ตฌ๋ฌธ ์•ž์— ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๊ฐ€ ์žˆ๋Š”์ง€๊ฐ€ ๊ด€๊ฑด


4. ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ์˜ this ์šฐํšŒ

4-1. ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๋‚ด๋ถ€ ์Šค์ฝ”ํ”„์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” this๋ฅผ ๋ณ„๋„์˜ ๋ณ€์ˆ˜(self)์— ํ• ๋‹น
var obj1 = {
	outer: function() {
		console.log(this); // (1) outer

		// AS-IS
		var innerFunc1 = function() {
			console.log(this); // (2) ์ „์—ญ๊ฐ์ฒด
		}
		innerFunc1();

		// TO-BE
		var self = this;
		var innerFunc2 = function() {
			console.log(self); // (3) outer
		};
		innerFunc2();
	}
};

// ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ถ€๋ถ„
obj1.outer();

4-2 ํ™”์‚ดํ‘œํ•จ์ˆ˜ (=this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜)

  • ES6์—๋Š” this ๋ฐ”์ธ๋”ฉ ๊ณผ์ • ์ž์ฒด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์ „์—ญ๊ฐ์ฒด๋กœ ๋ฐ”๋ผ๋ณด๋Š” ๋ฌธ์ œ ๋•Œ๋ฌธ์— ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ๋„์ž…ํ•จ
var obj = {
	outer: function() {
		console.log(this); // (1) obj
		var innerFunc = () => {
			console.log(this); // (2) obj
		};
		innerFunc();
	}
}

obj.outer();

4-3 ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๊ทธ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

์ฝœ๋ฐฑํ•จ์ˆ˜: ์–ด๋– ํ•œ ํ•จ์ˆ˜, ๋ฉ”์„œ๋“œ์˜ ์ธ์ž(๋งค๊ฐœ๋ณ€์ˆ˜)๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜

  • ์ฝœ๋ฐฑํ•จ์ˆ˜๋„ ํ•จ์ˆ˜์ด๊ธฐ์— this๋Š” ์ „์—ญ๊ฐ์ฒด
  • ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ๋ฐ›์€ ํ•จ์ˆ˜์—์„œ ์ฝœ๋ฐฑํ•จ์ˆ˜์— ๋ณ„๋„๋กœ this๋ฅผ ์ง€์ •ํ•œ ๊ฒฝ์šฐ -> ์˜ˆ์™ธ์ ์œผ๋กœ ๋Œ€์ƒ ์ฐธ์กฐ
// ๋ณ„๋„ ์ง€์ • ์—†์Œ : ์ „์—ญ๊ฐ์ฒด
setTimeout(function () { console.log(this) }, 300);

// ๋ณ„๋„ ์ง€์ • ์—†์Œ : ์ „์—ญ๊ฐ์ฒด
[1, 2, 3, 4, 5].forEach(function(x) {
	console.log(this, x);
});

// addListener ์•ˆ์—์„œ์˜ this๋Š” ํ•ญ์ƒ ํ˜ธ์ถœํ•œ ์ฃผ์ฒด์˜ element๋ฅผ returnํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Œ
// ๋”ฐ๋ผ์„œ this๋Š” button์„ ์˜๋ฏธํ•จ
document.body.innerHTML += '<button id="a">ํด๋ฆญ</button>';
document.body.querySelector('#a').addEventListener('click', function(e) {
	console.log(this, e);
});
  1. setTimeout ํ•จ์ˆ˜, forEach ๋ฉ”์„œ๋“œ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœํ• ๋•Œ this ์ง€์ •ํ•˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ window ๊ฐ์ฒด
  2. addEventListner ๋ฉ”์„œ๋“œ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ ์ž์‹ ์„ ์ƒ์†ํ•˜๋ฏ€๋กœ this๋Š” addEventListner์˜ ์•ž๋ถ€๋ถ„

5. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

5-1 ์ƒ์„ฑ์ž

  • ๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ผ์ข…์˜ ํ‹€

post-custom-banner

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