๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋Œ€์ „์ œ

  • this๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ฒฐ์ •๋œ๋‹ค.
  • ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ์—๋Š” this๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š๋Š”๋‹ค.
  • this์—๋Š” ํ˜ธ์ถœํ•œ ์ฃผ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด๋‹ค. 
  • ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•  ๋‹น์‹œ์— this๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณธ๋‹ค.
function func1() {
  console.log(this);
}

obj ={
	func1:func1,
}

func1();     // Window
obj.func1()  // obj

 

  • ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ์„œ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ์—๋งŒ ๋ฉ”์„œ๋“œ๋กœ ๋™์ž‘ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•œ๋‹ค.
  • this ๋ฐ”์ธ๋”ฉ์— ๊ด€ํ•ด์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋‹น์‹œ์˜ ์ฃผ๋ณ€ ํ™˜๊ฒฝ(๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์ธ์ง€, ํ•จ์ˆ˜ ๋‚ด๋ถ€์ธ์ง€)๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.
  • ์˜ค์ง ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์ƒ, ์•ž์— ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๊ฐ€ ๊ด€๊ฑด์ด๋‹ค. 
const obj = {
  method() {
    console.log(this);     // obj, obj.method()๋กœ์จ ํ˜ธ์ถœ๋จ(๋ฉ”์†Œ๋“œ)
    function innerMethod() {
      console.log(this);   // Window, innerMethod()๋กœ์จ ํ˜ธ์ถœ๋จ(ํ•จ์ˆ˜)
    }
    innerMethod();
  },
};

obj.method();
class Foo {
	method() {
    	console.log(this);
        [1,2].forEach(function(i) {
        	console.log(this); // forEach ๋‚ด๋ถ€์—์„œ callback()์œผ๋กœ ๋ถˆ๋ฆฌ๊ธฐ์— "ํ•จ์ˆ˜๋กœ์จ" ํ˜ธ์ถœ
        });
    }
}

const f = new Foo()
f.method();

// Foo, window, window

 

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ vs ์ผ๋ฐ˜ ํ•จ์ˆ˜

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ ์‹œ ์ƒˆ๋กœ์šด ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๋•Œ,  this ๋ฐ”์ธ๋”ฉ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ECMAScript ๋ช…์„ธ์— ๋”ฐ๋ฅด๋ฉด, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” Environment Record์—์„œ this ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค. 
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— this ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ƒ์œ„ ์Šค์ฝ”ํ”„๋กœ ์ฒด์ด๋‹ํ•ด์„œ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.
  •  
class Foo {
	method() {
    	console.log(this);
        [1,2].forEach((i) => {
        	console.log(this); // ()=>{}์—์„œ this๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ์Šค์ฝ”ํ”„ ์ฒด์ด๋‹ํ•ด์„œ ๊ฐ’์„ ์ฐพ์Œ
        });
    }
}

const f = new Foo()
f.method();

// Foo, Foo, Foo
const obj1 = {
  method() {
    console.log(this);
  },
};

const obj2 = {
  method: () => {
    console.log(this);  
  },
};
const func = obj1.method;

obj1.method(); // obj1
obj2.method(); // Window
func();        // Window

 

  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ this์— ๋ฐ”์ธ๋”ฉํ•  ๊ฐ์ฒด๊ฐ€ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์— this๊ฐ€ ์•„์˜ˆ ์—†์–ด, ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ this์— ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์— ์Šค์ฝ”ํ”„์ฒด์ธ์ƒ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด this๊ฐ€ ์ •์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.
const obj1 = {
  method() {
  console.log(this)           // obj1
    function innerMethod() {
      console.log(this);      // winodw
    }
    innerMethod();
  },
};

const obj2 = {
  method() {
  console.log(this)           // obj2
    const innerMethod = () => {
      console.log(this);      // obj2
    };
    innerMethod();
  },
};

const obj3 = {
  method: () => {
  console.log(this)           // window
    const innerMethod = () => {
      console.log(this);      // window
    };
    innerMethod();
  },
};

obj1.method();
obj2.method(); 
obj3.method();

 

์ค‘๊ฐ„์ •๋ฆฌ

1. ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜๋กœ์จ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ this๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š๋Š”๋‹ค.

2. ์ง€์ •๋˜์ง€ ์•Š๋Š” this๋Š” window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

3. But, ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋กœ์จ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ this๋Š” ํ˜ธ์ถœํ•œ ์ฃผ์ฒด์ธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

4. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋ฉ”์„œ๋“œ๋กœ์จ ํ˜ธ์ถœํ•˜์—ฌ๋„, ํ•จ์ˆ˜ ๋‚ด๋ถ€์— this๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. (๊ฐ€์žฅ ๊ฐ€๊นŒ์šด this๋ฅผ ์ฒด์ด๋‹ํ•˜๋Š” ๊ฒƒ)

const person = {
  name: 'Lee',
  sayHi() { // === sayHi: function() {
    console.log(`Hi ${this.name}`);
  }
};

person.sayHi(); // Hi Leeโ€‹
const person = {
  name: 'Lee',
  sayHi: () => console.log(`Hi ${this.name}`)
};

person.sayHi(); // Hi undefined(window.name)

 

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” call, apply, bind ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ this๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

window.x = 1;
const normal = function () { return this.x; };
const arrow = () => this.x;

console.log(normal.call({ x: 10 })); // 10
console.log(arrow.call({ x: 10 }));  // 1

normal์€ ํ•จ์ˆ˜๋กœ์จ ํ˜ธ์ถœ๋์ง€๋งŒ, callํ•จ์ˆ˜์— ์˜ํ•ด ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๊ฐ€ {x:10}์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

arrow ํ•จ์ˆ˜๋‚ด๋ถ€์— this๊ฐ€ ์•„์˜ˆ ์—†๊ธฐ ๋•Œ๋ฌธ์— thisArg๋ฅผ {x:10}์„ ์ฃผ์–ด๋„ ์ฐธ์กฐํ•˜์ง€ ๋ชปํ•œ๋‹ค.

 

(์ตœ์ข… ๋ณด์Šค) ๋ฉ”์†Œ๋“œ๋กœ์จ ํ˜ธ์ถœ๋˜์—ˆ์„ ๋•Œ this๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๊ฒฝ์šฐ

function test(){
    return {
      rs : 25,
      bar : function(){
        console.log(this.rs);
      }
    }
}

test.call({rs:100})        // {rs:25, bar:function}
test.call({rs:100}).bar(); // 25
 function test(){
    return {
      rs : 25,
      bar : () => {
        console.log(this.rs);
      }
    }
}
test.call({rs:100})        // {rs:25, bar:function}   
test.call({rs:100}).bar(); // 100

์œ„์˜ ์˜ˆ์‹œ๊ฐ€ ํ–‡๊ฐˆ๋ ธ๋˜ ์ด์œ 

  • test.call({rs:100}): ์ด ์ฝ”๋“œ๋Š” thisArg๊ฐ’์„ {rs:100}์œผ๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด์ง€, ๋ฆฌํ„ด ๊ฐ’ ์ž์ฒด๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.
  • test.call({rs:100})๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ์ฝ”๋“œ๋‹ค. ์ฆ‰, ์‹คํ–‰ ์ปจํ…์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ฝœ์Šคํƒ์— ์Œ“์—ฌ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค.
  • ๋”ฐ๋ผ์„œ test ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ this๋Š” {rs:100}์„ ๊ฐ–๋Š”๋‹ค

- ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ์จ ํ˜ธ์ถœ๋˜์–ด๋„, ๊ฐ์ฒด๋ฅผ this๋กœ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” test์˜ ๋ฆฌํ„ด ๊ฐ’, {rs:25, bar:f}์„ this๋กœ ๋ณด์ง€ ๋ชปํ•˜๊ณ , bar๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์ธ,ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ์ฝœ์Šคํƒ ์œ„์— ์žˆ๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ this๊ฐ’, {rs:100}์„ ์ฒด์ด๋‹ํ•œ๋‹ค.

 

- ์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ์จ ํ˜ธ์ถœ๋˜๋ฉด this๊ฐ’์€ ํ˜ธ์ถœ ๋Œ€์ƒ์„ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ๋•Œ๋ฌธ์— this๋Š” test์˜ ๋ฆฌํ„ด ๊ฐ’, {rs:25, bar:f}์„ ๋ฐ”๋ผ๋ณธ๋‹ค.

 

์˜คํ•ด๊ธˆ์ง€!

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ์ฝ๋‹ค๋ณด๋ฉด ์ด๋Ÿฐ ์ƒ๊ฐ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. "ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ this๋Š” call๊ณผ ๊ฐ™์€ ๋ฐ”์ธ๋”ฉ ๋ฉ”์„œ๋“œ๋กœ ๊ฐ’์ด ๋ณ€ํ•  ์ˆ˜ ์žˆ๊ตฌ๋‚˜!" BullShit! ์ ˆ๋Œ€ ์•„๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์— this ๋ฐ”์ธ๋”ฉ ๊ณผ์ •์ด ์ƒ๋žต๋˜์–ด ์žˆ์–ด, ํ˜ธ์ถœ ๋Œ€์ƒ์„ this ๊ฐ’์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ทธ์ € ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๋ฐ”๋ผ๋ณผ ๋ฟ์ด์ง€, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์— ๋ฐ”์ธ๋”ฉ ๋ฉ”์„œ๋“œ๋Š” ์ „ํ˜€ ์˜๋ฏธ ์—†๋‹ค. ๋‹จ์ง€ ์œ„์˜ ์˜ˆ์‹œ๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ this๊ฐ’์„ ์ฐพ์œผ๋Ÿฌ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ฐธ์กฐํ•˜๋ ค๊ณ  ํ•  ๋•Œ, ๋ฐ”์ธ๋”ฉ ๋ฉ”์„œ๋“œ์˜ this๋ฅผ ๋งŒ๋‚˜ ๊ทธ ๊ฐ’์„ ์ฐธ์กฐํ–ˆ์„ ๋ฟ์ด๋‹ค. 

 

์ด์™€ ๋‹ฌ๋ฆฌ, ์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์— this ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ณผ์ •์ด ์žˆ๊ณ , test()์˜ ๋ฉ”์„œ๋“œ๋กœ์จ ํ˜ธ์ถœ๋๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœ๋Œ€์ƒ์ด์ž, ๋ฆฌํ„ด ๊ฐ’์ธ ๊ฐ์ฒด๋ฅผ this๋กœ ๋ฐ”๋ผ๋ณผ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. 

function test(){
    return {
      rs : 25,
      bar : function(){
        console.log(this.rs);
      }
    }
}

test().bar()
 function test(){
    return {
      rs : 25,
      bar : () => {
        console.log(this.rs);
      }
    }
}
test().bar()

 

๊ณต๋ถ€ํ–ˆ๋˜ ์ž๋ฃŒ์— ํ† ๋‹ฌ์•„ ๋ณด๊ธฐ

1. ES5 function ์•ˆ์˜ this๋Š” ์ž์‹ ์„ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ’ˆ๊ณ  ์žˆ๋Š” scope๋กœ ํ•ญ์ƒ ๋ณ€ํ•œ๋‹ค.

=> this๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋‹ค. function๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ƒ์„ฑ๋˜๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ this๊ฐ’์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


2. arrow Function์€ ์ฒ˜์Œ ๋ฐ”์ธ๋”ฉ ๋œ ์Šค์ฝ”ํ”„ ์•ˆ์—์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” this๊ฐ€ ์ ˆ๋Œ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

=> ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์˜ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ์˜ this๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

 

3. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ this๋Š” ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜๋ฅผ ๋‘˜๋Ÿฌ์‹ผ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

=> ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ ์ƒ์„ฑ ์‹œ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ณผ์ •์ด ์ œ์™ธ๋˜๋ฏ€๋กœ, ์Šค์ฝ”ํ”„ ์ฒด์ด๋‹์„ ํ†ตํ•˜์—ฌ ์ƒ์œ„ ์ปจํ…์ŠคํŠธ์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

 

4. ์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ this๋Š” ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ๋œ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

=>์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ this๋Š” ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์Šค์ฝ”ํ”„ ์ฒด์ด๋‹์„ ํ†ตํ•˜์—ฌ ๋ฐ”์ธ๋”ฉ๋˜๊ณ , ๋ฉ”์†Œ๋“œ๋กœ ํ˜ธ์ถœ๋˜๋ฉด ๊ทธ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.

 

 

"use strict"

  • Without "use strict": this in a standalone function call refers to the global object. (Window, global)
  • With "use strict": this in a standalone function call is undefined.

 

๋ฐ˜์‘ํ˜•