๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“š Study Note/JavaScript

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive - this

by Jellll_y 2022. 12. 25.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

this ์ •์˜

- ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜๋‹ค. 

- this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“ค๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

this ์‚ฌ์šฉ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ์˜ํ•ด ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.

 

- ์ „์—ญ์—์„œ this 

this๋Š” ์–ด๋””์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ „์—ญ์—์„œ์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

console.log(this) // window

 

- ์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this

์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this๋ฅผ ์‚ฌ์šฉ ์‹œ ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

ํ•˜์ง€๋งŒ strict mode์ผ ๊ฒฝ์šฐ undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์ผ๋ฐ˜ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด this๋Š” ์ „์—ญ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

ES6๋ถ€ํ„ฐ ๊ฐ์ฒด ์•ˆ์˜ ํ•จ์ˆ˜ ํ”„๋กœํผํ‹ฐ ์ถ•์•ฝํ˜•์€ ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ด์™ธ์—๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์ด๋‹ค.

function fn(){
    console.log(this) // window
}

function fn2(){
    'use strict' 
    console.log(this) // undefined
}

const obj = {
    foo(){
        function bar() {
        	console.log(this) // window
        }
    }
}

 

- ๋ฉ”์„œ๋“œ์—์„œ this 

๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ this๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ ๋ชจ๋“  ํ•จ์ˆ˜(์ค‘์ฒฉ ํ•จ์ˆ˜, ์ฝœ๋ฐฑ ํ•จ์ˆ˜) ๋‚ด๋ถ€์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

var x = 'global' // window.x = 'global'

const obj = {
    x: 'local',
    foo(){
    	console.log(this) // {x: 'local', foo: f}
        console.log(this.x) // 100

        // ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ์ผ๋ฐ˜ํ•จ์ˆ˜
        function bar(){
            console.log(this) // window
            console.log(this.x) // 'global'
        }
        bar()
        
        // ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— this๋Š” window๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
        setTimeout(function(){
            console.log(this) // window
            console.log(this.x) // 'global'
        }, 100)
    },
}

obj.foo()

์œ„์˜ setTimeout์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๊ฐ€ ์ „์—ญ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ ค๋ฉด ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋˜๋Š” ๋ช…์‹œ์ ์œผ๋กœ this ๊ฐ์ฒด ์ „๋‹ฌ(bind) ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

var x = 'global' // window.x = 'global'

const obj = {
    x: 'local',
    foo(){
    	// ์ฝœ๋ฐฑํ•จ์ˆ˜ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ
        setTimeout(()=>{
            console.log(this.x) // 'local'
        }, 1000)
        
        // ๋ช…์‹œ์ ์œผ๋กœ this ๊ฐ์ฒด ์ „๋‹ฌ
        setTimeout(function(){
			console.log(this.x) // 'local'
		}.bind(this),1000)
    },
}

obj.foo()

 

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

- ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

function Circle(radius){
    this.radius = radius
	this.getDiameter = function () {
        return 2 * this.radius
    }
}

const circle1 = new Circle(5)

console.log(circle1.getDiameter()) // 10