This๋ž€?(Call, Bind, Apply)

Blackeichiยท2022๋…„ 10์›” 1์ผ
0
post-thumbnail
post-custom-banner

#This๋ž€?๐Ÿ“Œ

๊ฐ์ฒด์ง€ํ–ฅ์–ธ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ this๋ผ๋Š” ์˜ˆ์•ฝ์–ด๋Š” ํ•จ์ˆ˜๊ฐ€ ์†ํ•ด ์žˆ๋Š” ๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ด๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์ž๊ธฐ ์ž์‹ ์ด๋ผ๋Š” ๋ง์ด ์ƒ๋‹นํžˆ ๋ชจํ˜ธํ•˜๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ผ๊ธ‰ ๊ฐ์ฒด์ธ๋ฐ, ์ผ๊ธ‰๊ฐ์ฒด์˜ ์ •์˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๋ณ€์ˆ˜๋‚˜ ๋ฐ์ดํ„ฐ์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค.
๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋กœ์„œ ๋ฆฌํ„ด๋  ์ˆ˜ ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ง•์œผ๋กœ ์ธํ•ด, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๋Š” ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์—์„œ ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ this๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค. ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

const person = {
	name : "JeongWoo",
    whoami:function() {
    //์ด๋ฆ„ : function()์€ ๋ฉ”์†Œ๋“œ
    	console.log(this);
    }
};
person.whoami();
const myname = person.whoami;
myname();

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ดค์„ ๋•Œ, ๊ฒฐ๊ณผ์ ์œผ๋ก  person.whoami();์™€ myname();๊ฐ€ ๊ฐ™๋‹ค.
ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ๋Š”, person.whoami()๋Š” ๊ฐ์ฒด์ธ person์„ ์ถœ๋ ฅํ•˜๊ณ  myname()์€ window๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

this๊ฐ€ ๊ฒฐ๊ตญ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ(๊ฐ์ฒด)์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค(this === ํ˜ธ์ถœํ•œ ๋Œ€์ƒ)
person.whoami()์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๋Š” whoami๋ฅผ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ์€ person์ด๊ธฐ์— this๊ฐ€ person์„ ๊ฐ€๋ฆฌํ‚ค๊ณ , whoami๋ฅผ ๋ถˆ๋Ÿฌ๋‚ด๋Š” myname()๋ฅผ ํ˜ธ์ถœํ•œ ๋Œ€์ƒ์€ ์ „์—ญ(window)์ด๊ธฐ ๋•Œ๋ฌธ์— window๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

#Bind๋ž€?๐Ÿ“Œ

this๋ฅผ ๋ˆ„๊ฐ€ ํ˜ธ์ถœํ•˜๋Š”๊ฐ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ this๋ฅผ ๊ณ ์ •์‹œํ‚ค๋Š” ๊ฒƒ์„ bind๋ผ๊ณ  ํ•œ๋‹ค.

	const person = {
    	name : "JeongWoo"
    }
  	function whoami (){
    	console.log(this);
  	}
  	const myname = whoami.bind(person);
    whoami();
    //window ์ถœ๋ ฅ
	myname();

์œ„์˜ ์ฝ”๋“œ๋Š” bind๋ฅผ ์ด์šฉํ•˜์—ฌ myname์˜ this๋ฅผ person์œผ๋กœ ๊ณ ์ •์‹œํ‚จ ๊ฒƒ์ด๊ณ , ์‹คํ–‰์‹œํ‚ค๋ฉด person์ด ์ถœ๋ ฅ์ด ๋œ๋‹ค.

๊ฐ์ฒด์˜ ์ •๋ณด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

	const person = {
    	name : "JeongWoo"
    }
  function whoami (birth, gender){
    	this.birth = birth;
      this.gender = gender;
      console.log(this)
  };
  const myname = whoami.bind(person, 95, "๋‚จ");
  myname();

#Call์ด๋ž€?๐Ÿ“Œ

Call์€ ๋ชจ๋“  ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, this๋ฅผ ํŠน์ •๊ฐ’์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด call์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค.

	const person = {
    	name : "JeongWoo"
    }
  	function whoami (){
    	console.log(this);
  	}
  	whoami();
  	whoami.call(person);

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด whoami();๋Š” window๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ , whoami.call(person)๋Š” call์„ ์‚ฌ์šฉํ•˜์—ฌ this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋ฅผ ๋„˜๊ธฐ๋ฏ€๋กœ person์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋œ๋‹ค.

๋˜ํ•œ ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—…๋ฐ์ดํŠธํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

	const person = {
    	name : "JeongWoo"
    }
  function whoami (birth, gender){
    	this.birth = birth;
      this.gender = gender;
      console.log(this)
  };
  whoami.call(person, 95, "๋‚จ");

#Apply๋ž€?๐Ÿ“Œ

apply๋Š” ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›์œผ๋ฉฐ, ๊ทธ ์™ธ์—๋Š” Call๊ณผ ์™„์ „ํžˆ ๊ฐ™๋‹ค.

	const person = {
    	name : "JeongWoo"
    }
  function whoami (birth, gender){
    	this.birth = birth;
      this.gender = gender;
      console.log(this)
  };
  whoami.apply(person, [95, "๋‚จ"]);
profile
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ํ•œ์ •์šฐ์ž…๋‹ˆ๋‹ค. ๐Ÿ˜
post-custom-banner

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