Javascript/Javascript

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] crazy this, ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this ์˜ˆ์ œ ์ด์ •๋ฆฌ

Rainbow๐ŸŒˆCoder 2022. 5. 22. 02:50
728x90

https://poiemaweb.com/es6-arrow-function

 

Arrow function | PoiemaWeb

Arrow function(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)์€ function ํ‚ค์›Œ๋“œ ๋Œ€์‹  ํ™”์‚ดํ‘œ(=>)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋žตํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

poiemaweb.com

ใ„ด> ๊ฐ€์žฅ ์ •ํ™•ํ•˜๊ณ  ์ž์„ธํ•˜๊ณ  ํ’๋ถ€ํ•œ ์˜ˆ์‹œ๋กœ ์„ค๋ช…๋˜์–ด ์žˆ๋Š” ๋ฌธ์„œ

const obj = {
	name: '๊ผฌ๋ถ€๊ธฐ',
	sayName: () => {
		console.log(this.name);
	}
}
obj.sayName();

class Counter {
	name = 'counter';
	count = 0;
	increase = () => {
		console.log(this);
	}
	obj = {
		name: '๊ผฌ๋ถ€๊ธฐ',
		sayName: () => {
			console.log(this.name);
		}
	}

}
const counter = new Counter();
console.log('counter.increase()');
counter.increase(); //this ๋Š” Counter
counter.obj.sayName();  //this๋Š” counter

temp.js:4 
temp.js:24 counter.increase()
temp.js:13 Counter
temp.js:18 counter

 

 

๋‹ค๋ฅธ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ this ๋ž€ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค(์˜ค๋ธŒ์ ํŠธ) ์ž์‹ ์„ ๊ฐ€๋ฅด์ผฐ๋‹ค.

  • this๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜(self-reference variable)์ด๋‹ค.
  • this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด ๊ทธ ์ž์‹ ์ด์—ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ this ๋Š” ์•Œ๋ผ๋”˜ ์š”์ˆ ๋žจํ”„์˜ ์ง€๋‹ˆ(;;;)์ด๋‹ค.

๋ˆ„๊ฐ€ ๋ถ€๋ฅด๋ƒ์— ๋”ฐ๋ผ์„œ this์˜ ์ฃผ์ธ์ด ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋ˆ„๊ฐ€ ํ˜ธ์ถœํ–ˆ๋Š”์ง€,

ํ˜ธ์ถœ ๋ฌธ๋งฅ์— ๋”ฐ๋ผ์„œ this๊ฐ€ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์–ธ์–ด์— ๋น„ํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ this๋Š” ์œ ๋… ๋” ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋˜ ๋‹ค๋ฅด๋‹ค.

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์„ ์–ธํ•œ ์‹œ์ ์˜ ๋ถ€๋ชจ ์Šค์ฝ”ํ”„๊ฐ€ this์ด๋‹ค. ๊ณ ๋กœ ๋ถˆ๋ณ€์ด๋‹ค.(;;;)

 

console.log(this); //this ๋Š” window
function func() {
	console.log(this);
}
func(); //this ๋Š” window
window.func(); //์ƒ๋™, ๊ธ€๋กœ๋ฒŒ์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์œˆ๋„์šฐ์—์„œ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ

class Counter {
	count = 0;
	increase = function () {
		console.log(this);
	}

}
const counter = new Counter();
counter.increase(); //this ๋Š” Counter

const tempIncrease = counter.increase;
tempIncrease(); //this ๋Š” undefined

const counter = new Counter();
counter.increase(); //this ๋Š” Counter

Counter์—์„œ increase()๋ฅผ ํ˜ธ์ถœํ–ˆ์œผ๋ฏ€๋กœ ์—ฌ๊ธฐ๊นŒ์ง€๋งŒ ํ•ด๋„ this๋Š” ํด๋ž˜์Šค๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ์—ˆ์œผ๋‚˜,

 

const tempIncrease = counter.increase;
tempIncrease(); //this ๋Š” undefined

tempIncrease๋ผ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ ์ˆœ๊ฐ„,

๊ทธ ์–ด๋– ํ•œ ๊ฐ์ฒด๊ฐ€ tempIncrease()๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ์— this์˜ ์ฃผ์ธ์€ ์ƒ์‹ค๋˜๊ณ 

undefined๊ฐ€ ์ถœ๋ ฅ๋˜๊ฒŒ ๋œ๋‹ค.(let๊ณผ const๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ์œˆ๋„์šฐ์— ๋“ฑ๋ก๋˜์–ด์žˆ์ง€ ์•Š๋‹ค. ๊ทธ๋ž˜์„œ ์œˆ๋„์šฐ๊ฐ€ ํ˜ธ์ถœ ํ•˜๋Š” ๊ฒƒ๋„, ๊ทธ ์–ด๋– ํ•œ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๋„ ์•„๋‹Œ ๊ฒƒ์ด ๋œ ๊ฒƒ์ด๋‹ค.)

 

console.log(this); //this ๋Š” window
function func() {
	console.log(this);
}
func(); //this ๋Š” window

class Counter {
	count = 0;
	increase = function () {
		console.log(this);
	}

}
const counter = new Counter();
counter.increase(); //this ๋Š” Counter

const tempIncrease = counter.increase;
tempIncrease(); //this ๋Š” undefined

class CrazyJs {
}
const crazyJavascript = new CrazyJs();
crazyJavascript.func = counter.increase;

crazyJavascript.func(); //this๋Š” CrazyJs

 

์œ„์˜ ์ƒํ™ฉ์—์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ฐ”๊พธ์–ด๋ณด์•˜๋‹ค.

class Counter {
	count = 0;
	increase = () => {
		console.log(this);
	}

}
const counter = new Counter();
console.log('counter.increase()');
counter.increase(); //this ๋Š” Counter

const tempIncrease = counter.increase;
console.log('tempIncrease()');
tempIncrease(); //this ๋Š” undefined

class CrazyJs {
}
const crazyJavascript = new CrazyJs();
crazyJavascript.func = counter.increase;

console.log('crazyJavascript.func()');
crazyJavascript.func(); //this๋Š” CrazyJs

 

counter.increase()
Counter


tempIncrease()
Counter


crazyJavascript.func()
Counter

 

์—ฌ๊ธฐ์„œ ์•„์ฃผ ์ค‘์š”ํ•œ ํฌ์ธํŠธ๋Š”, ์—ฌํƒ€ this๋Š” ํ˜ธ์ถœ ์‹œ์ ์— ๊ฒฐ์ •๋˜๋Š”๋ฐ ํ™”์‚ดํ‘œํ•จ์ˆ˜๋Š” ๊ทธ์ € ์„ ์–ธํ•œ ๋ถ€๋ชจ ์Šค์ฝ”ํ”„๋ฅผ ๋Š˜ this๋กœ ์œ ์ง€ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

 

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ : ๋ถ€๋ชจ ์Šค์ฝ”ํ”„(์„ ์–ธ ๊ธฐ์ค€)

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋„ this๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.

 

 

//๊ฐ์ฒด ์•ˆ์— ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์•ˆ์— ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” ์ผ€์ด์Šค
const obj = {
	name: '๊ผฌ๋ถ€๊ธฐ',
	sayName() {
		console.log(this.name);
		const inner = () => {
			console.log(this.name);
			console.log(this.name === window.name);
		}
		inner(); //ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ์‹œ this๋Š” ๋ถ€๋ชจ ์Šค์ฝ”ํ”„ ๊ธฐ์ค€
	}
}
obj.sayName();

//๊ฐ์ฒด ์•ˆ์— ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์•ˆ์— ์ผ๋ฐ˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š” ์ผ€์ด์Šค
const obj2 = {
	name: '๊ผฌ๋ถ€๊ธฐ2',
	sayName2() {
		console.log(this.name);
		function inner() {
			console.log(this.name);
			console.log(this.name === window.name);
		}
		inner(); //์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ this๋ฅผ ๋ฐ”๊พธ๋Š” ๋™์ž‘์ด ์•„๋ฌด ๊ฒƒ๋„ ์—†์œผ๋ฏ€๋กœ this๋Š” window
		debugger;
	}
}
obj2.sayName2();

๊ผฌ๋ถ€๊ธฐ
๊ผฌ๋ถ€๊ธฐ
false


๊ผฌ๋ถ€๊ธฐ2

true

 

const person = {
	name: "ํ™๊ธธ๋™",
	sayName: () => {
		console.log(this.name);
		const inner = () => {
			console.log(this.name);
			console.log(this.name === window.name);
		};
		inner();
	},
};

//ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์ผ ๊ฒฝ์šฐ this๋Š” window. 
person.sayName(); //''
console.log(window.name); //''

const obj = {
	name: '๊ผฌ๋ถ€๊ธฐ',
	sayName() {
		console.log(this.name);
		const inner = () => {
			console.log(this.name);
			console.log(this.name === window.name);
		}
		inner(); //ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ์‹œ this๋Š” ๋ถ€๋ชจ ์Šค์ฝ”ํ”„ ๊ธฐ์ค€
	}
}
obj.sayName();


const obj2 = {
	name: '๊ผฌ๋ถ€๊ธฐ2',
	sayName2() {
		console.log(this.name);
		function inner() {
			console.log(this.name);
			console.log(this.name === window.name);
		}
		inner(); //์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ this๋ฅผ ๋ฐ”๊พธ๋Š” ๋™์ž‘์ด ์•„๋ฌด ๊ฒƒ๋„ ์—†์œผ๋ฏ€๋กœ this๋Š” window
		debugger;
	}
}
obj2.sayName2();



true

๊ผฌ๋ถ€๊ธฐ
๊ผฌ๋ถ€๊ธฐ
false
๊ผฌ๋ถ€๊ธฐ2

true

 

const obj = {
	name: '๊ผฌ๋ถ€๊ธฐ',
	sayName: () => {
		console.log(this.name);
		function inner() {
			console.log(this.name);
			console.log(this.name === window.name);
		}
		inner(); //ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ์‹œ this๋Š” ๋ถ€๋ชจ ์Šค์ฝ”ํ”„ ๊ธฐ์ค€
	}
}
obj.sayName();

 


true

 

<์š”์•ฝ>

this ํ•จ์ˆ˜ ์„ ์–ธ์ด ์•„๋‹Œ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ, ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋งˆ๋‹ค ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ this๋Š” ์„ ์–ธ์‹œ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋‹ค๋ฅผ ์ˆ˜๊ฐ€ ์—†๋‹ค.

๊ธฐ๋ณธ this๋Š” window๋‹ค.

 

๋ฐ”์ธ๋”ฉ: this์˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ this๊ฐ€ ํŠน์ • '๊ฐ์ฒด'์— ์—ฐ๊ฒฐ๋˜๋Š” ๊ฒƒ. 

 

this๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋‹ฌ๋ผ์ง„๋‹ค(๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„์ฒ˜๋Ÿผ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ์ˆœ๊ฐ„์ด ์•„๋‹˜)
๊ธฐ๋ณธ์€ window์ด๊ณ , this๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ–‰์œ„(ํ˜ธ์ถœ)์„ ํ–ˆ๋Š”์ง€ ์•ˆ ํ–ˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•ด์• ํ•œ๋‹ค.

<this>
๊ธฐ๋ณธthis, globalthis : ์ „์—ญ, window
strict mode : undefined
ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ : ๋ถ€๋ชจ ์Šค์ฝ”ํ”„(์„ ์–ธ ๊ธฐ์ค€)
๊ฐ์ฒด.๋ฉ”์„œ๋“œ : ๊ฐ์ฒด
new ํ˜ธ์ถœ : ๊ฐ์ฒด
call, apply, bind : this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด ๋ณ€๊ฒฝ๊ฐ€๋Šฅ

2. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ : ๋ถ€๋ชจ ์Šค์ฝ”ํ”„(์„ ์–ธ ๊ธฐ์ค€)

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋„ this๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.

 

const person = {
  name: "ํ™๊ธธ๋™",
  sayName: () => {
    console.log(this.name);
  },
};

//ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์ผ ๊ฒฝ์šฐ this๋Š” window. 
person.sayName(); //''
console.log(window.name); //''
 

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

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์™€ ๋ฐ”์ธ๋”ฉ

new๋ฅผ ๋ถ™์—ฌ์„œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ, this๋Š” ๊ฐ์ฒด ์ž๊ธฐ์ž์‹ 

 

 

4. this๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ฐฉ๋ฒ• bind, apply, call

Call, Apply, Bind ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ์‹œ ๋ฉ”์„œ๋“œ์— ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ

function sayName() {
  console.log(this.name);
}
sayName(); //window.name์ด๋ฏ€๋กœ ''
sayName.bind({ name: "ํ™ฉ์ง„์ด" })(); //ํ™ฉ์ง„์ด
sayName.apply({ name: "๋‘๊บผ๋น„" });//๋‘๊บผ๋น„
sayName.call({ name: "ํƒ๊ด€์˜ค๋ฆฌ" }); //ํƒ๊ด€์˜ค๋ฆฌ
  • bind : ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ฌ, ๊ณ ๋กœ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๊ฐ™์ด ํ•ด์ฃผ์–ด์•ผ ํ•จ

sayName.bind({ name: "ํ™ฉ์ง„์ด" })(); //ํ™ฉ์ง„์ด : this๋ฅผ { name: "ํ™ฉ์ง„์ด" } ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค.

  • apply : bind์—์„œ ์‹คํ–‰๋ถ€()๋งŒ ์ œ์™ธํ•˜๋ฉด ๋จ. ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์ž๋™์œผ๋กœ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ

sayName.apply({ name: "๋‘๊บผ๋น„" });//๋‘๊บผ๋น„ : this๋ฅผ { name: "๋‘๊บผ๋น„" } ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค.

  • call

sayName.call({ name: "ํƒ๊ด€์˜ค๋ฆฌ" }); //ํƒ๊ด€์˜ค๋ฆฌ : this๋ฅผ { name: "ํƒ๊ด€์˜ค๋ฆฌ" }์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด๋‹ค.

 

์ฝ”๋“œ


1. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ this๋Š” ์ „์—ญ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ

console.log(this === window); // true

a = 30;
console.log(window.a); // 30

function x(){
  return this;
}

x() === window; // true

2. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์‹œ this๋Š” ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ

 
let john = {
    firstName: "John",
    lastName: "Doe",
    driveCar() {
        console.log(this.firstName + " drives a car.")
    }
}

john.driveCar(); // John drives a car

3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ์‹œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ

 
function person() {
    this.firstName = "John",
    this.lastName = "Doe",
    this.start = function() {
        console.log(this.firstName + "drives a car.")
    }
}

let person1 = new person();

console.log(person1); // { firstName: 'John', lastName: 'Doe', start: [Function] }โ€‹

 

4. Call, Apply, Bind ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ์‹œ ๋ฉ”์„œ๋“œ์— ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ

  • call
function print( a, b, c ){
    console.log(this.name);
    console.log(a + b + c);
}

const person = {
    name: 'ํ™๊ธธ๋™'
};

print.call(person,1,2,3);
//ํ™๊ธธ๋™
//6

call ์‚ฌ์šฉ์‹œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ•จ์ˆ˜์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด์— this๋ฅผ ๋ฐ”์ธ๋”ฉ

 

  • apply
function print( a, b, c ){
    console.log(this.name);
    console.log(a + b + c);
}

const person = {
    name: 'ํ™๊ธธ๋™'
};

print.apply(person,[1,2,3] ); 
//ํ™๊ธธ๋™
//6

apply๋„ call๊ณผ ๊ฑฐ์˜ ๊ฐ™์€ ์“ฐ์ž„์ƒˆ๋ฅผ ๋ณด์ด์ง€๋งŒ ์ธ์ž๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ „๋‹ฌ.

 

  • bind
function logAge ( a, b, c ){

  console.log(this.age);
  console.log(a + b + c);
}

const person = {
    age: 5
};

const myAge = logAge.bind(person);
myAge(1,2,3); 
//5
//6

bind์˜ ๊ฒฝ์šฐ๋Š” ์ฒซ๋ฒˆ์งธ์ธ์ž์— this๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค๋Š” ์ ์€ ๊ฐ™์ง€๋งŒ, ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ฆ‰, ๋ฐ˜ํ™˜๋œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์•ผ ์›๋ณธ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

 

์š”์•ฝ์ •๋ฆฌ


์ด์ฒ˜๋Ÿผ this๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •.

ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ,

๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ์ด๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ,

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ

Call, Apply, Bind ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ์‹œ ๋ฉ”์„œ๋“œ์— ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ

 

 

์ฐธ๊ณ ๋ฌธํ—Œ:

[JS] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ this

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this ์˜ ๊ฐœ๋…๊ณผ ๋ฐ”์ธ๋”ฉ ์„ค๋ช…

Global scope - MDN Web Docs Glossary: Definitions of Web-related terms | MDN

PoiemaWeb

ํ•จ์ˆ˜ ๋ฐ”์ธ๋”ฉt

 

728x90