๐Ÿ’ป TIL 23.03.07

๊น€์˜์šฐ(Yeongwoo Kim)ยท2023๋…„ 3์›” 9์ผ
0
post-thumbnail

๐Ÿคท ๋ฉด์ ‘ ๋Œ€๋น„ ๊ณต๋ถ€๋ฅผ ์ด์–ด์„œ ํ•˜๋ ค ํ•œ๋‹ค.

1. This

  • JS์—์„œ ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜ ๋‚ด๋ถ€์— this๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋˜๋Š”๋ฐ, ํ•ด๋‹น ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋ฐ”์ธ๋”ฉ ๋˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

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

  1. ํ•จ์ˆ˜ ํ˜ธ์ถœ
  2. ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
  3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
  4. apply/call/bind ํ˜ธ์ถœ
var foo = function () {
  console.dir(this);
};

// 1. ํ•จ์ˆ˜ ํ˜ธ์ถœ
foo(); // window
// window.foo();

// 2. ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
var obj = { foo: foo };
obj.foo(); // obj

// 3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
var instance = new foo(); // instance

// 4. apply/call/bind ํ˜ธ์ถœ
var bar = { name: 'bar' };
foo.call(bar);   // bar
foo.apply(bar);  // bar
foo.bind(bar)(); // bar

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

  • ์ „์—ญ ๊ฐ์ฒด๋Š” ๋ชจ๋“  ๊ฐ์ฒด์˜ ์ตœ์ƒ์œ„ ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ๋Š” window, terminal ์ƒ์—์„œ๋Š” global์ด๋‹ค.
  • this๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ „์—ญ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋˜๋Š”๋ฐ, ํ•จ์ˆ˜์•ˆ์— ์žˆ๋Š” ๋‚ด๋ถ€ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ์—๋„ this๋Š” ์™ธ๋ถ€ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ „์—ญ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
var foo = function(){
	var boo = function(){
    	console.dir(this);
    }
    boo() // window
}
foo();

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

  • ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด์˜ value์ด๋ฉด ๋ฉ”์†Œ๋“œ๋กœ์„œ ํ˜ธ์ถœ๋˜๋Š”๋ฐ, ์ด๋•Œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.
var object1 = {
	name: "Kim",
    sayName: function(){
    	console.log(this.name);
    }
}
var object2 = {
	name: "Pee"
}

object2.sayName = object1.sayName;

object1.sayName(); // Kim
object2.sayName(); // Pee

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

  • JS์˜ ์ƒ์„ฑ์ž๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์— new ์—ฐ์‚ฐ์ž๋ฅผ ๋ถ™์—ฌ ํ˜ธ์ถœํ•˜๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋™์ž‘ํ•œ๋‹ค.
function Person(name) {
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ฝ”๋“œ ์‹คํ–‰ ์ „ -------- 1
  this.name = name;  // --------- 2
  // ์ƒ์„ฑ๋œ ํ•จ์ˆ˜ ๋ฐ˜ํ™˜ -------------- 3
}

var me = new Person('Lee');
console.log(me.name); // Lee

4. apply/call/bind ํ˜ธ์ถœ

  • JS์—”์ง„์€ ์•”๋ฌต์ ์œผ๋กœ this๋ฅผ 1~3๋ฒˆ์˜ ๊ธฐ์ค€์œผ๋กœ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ ์ด์™ธ์—๋„ ์ง์ ‘ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•˜๋Š”๋ฐ apply์™€ call์ด ๊ทธ๋Ÿฌํ•˜๋‹ค.

  • ๊ทธ๋Ÿฌ๋‚˜ ๋ช…์‹œ์ ์ธ ๋ฐ”์ธ๋”ฉ์ด๋ผ๊ณ  ํ•˜๋”๋ผ๋„ apply์™€ call์€ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์— global context๋กœ ๋ฐ”์ธ๋”ฉ์ด๋œ๋‹ค. ์ด๋•Œ apply์™€ call์€ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜๊ตฌ์ ์œผ๋กœ this๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•  ์ˆ˜ ์—†๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

  • ๋ฐ˜๋ฉด์— bind() ๋ฉ”์„œ๋“œ๋Š” this๊ฐ€ bind์— ์ œ๊ณต๋œ ๊ฐ’์œผ๋กœ ์„ค์ •๋œ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, ํ•ด๋‹น ํ•จ์ˆ˜์˜ ์ธ์ž๊ฐ’์˜ ์ˆœ์„œ ๋˜ํ•œ ์˜๊ตฌ์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

var healthObj = {
	name : "๋‹ฌ๋ฆฌ๊ธฐ",
	lastTime : "PM10:12",
	showHealth : function() {
        setTimeout(function(){
            console.log(this.name + "๋‹˜, ์˜ค๋Š˜์€ " + this.lastTime + "์— ์šด๋™์„ ํ•˜์…จ๋„ค์š”");
        }, 500);
    }
}

healthObj.showHealth();

ํ•ด๋‹น ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์–ธ๋œป ๋ณด๋ฉด "๋‹ฌ๋ฆฌ๊ธฐ๋‹˜ ์˜ค๋Š˜์€ PM10:12์— ์šด๋™์„ ํ•˜์…จ๋„ค์š”"๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํ•ด๋‹น ๊ฒฐ๊ณผ ๊ฐ’์€ "undefined๋‹˜, ์˜ค๋Š˜์€ undefined์— ์šด๋™์„ ํ•˜์…จ๋„ค์š”"์ด๋‹ค.
์™œ๋ƒํ•˜๋ฉด setTimeout์€ ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋Š” ์ด๋ฏธ showHealth๊ฐ€ ๋ฐ˜ํ™˜๋œ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

var healthObj = {
	name : "๋‹ฌ๋ฆฌ๊ธฐ",
	lastTime : "PM10:12",
	showHealth : function() {
        setTimeout(runFunction, 500);
    }
}
var runFunction = function(){
	console.log(this.name + "๋‹˜, ์˜ค๋Š˜์€ " + this.lastTime + "์— ์šด๋™์„ ํ•˜์…จ๋„ค์š”");
}.bind(healthObj);

healthObj.showHealth();

์œ„์™€ ๊ฐ™์ด runFunction์— healthObj์— bind๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด, this๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ๋ฐ”์ธ๋“œํ•˜๊ฒŒ ๋œ๋‹ค.

โค๏ธ ๋„์›€๋ฐ›์€ ๊ณณ

https://poiemaweb.com/js-this
https://enai.tistory.com/14

profile
์ฐจ๊ทผ์ฐจ๊ทผ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค

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