250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์ปดํฌ์ง€์…˜
  • ์œ„์ž„

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

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

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

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

'Javascript > Javascript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋งค๊ฐœ๋ณ€์ˆ˜, ์ธ์ž ์šฉ์–ด ์ฐจ์ด  (0) 2022.05.27
[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ์ต๋ช…ํ•จ์ˆ˜, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜, ์ฝœ๋ฐฑํ•จ์ˆ˜  (0) 2022.05.22
[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] export default  (0) 2022.05.17
[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋ฐ”๋‹๋ผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐฌ์„ฑ  (0) 2022.05.10
[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] module ์‚ฌ์šฉ๋ฒ• 3๊ฐ€์ง€  (0) 2022.05.09
    'Javascript/Javascript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋งค๊ฐœ๋ณ€์ˆ˜, ์ธ์ž ์šฉ์–ด ์ฐจ์ด
    • [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ์ต๋ช…ํ•จ์ˆ˜, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜, ์ฝœ๋ฐฑํ•จ์ˆ˜
    • [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] export default
    • [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋ฐ”๋‹๋ผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐฌ์„ฑ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”