let's get IT with DAVINA ๐Ÿ’ป

This ๋ณธ๋ฌธ

DEV_IN/JavaScript

This

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 28. 18:35
JS์—์„œ this๋Š” ์™œ ์–ด๋ ค์šธ๊นŒ?
๋‹ค๋ฅธ ์–ธ์–ด๋“ค์—์„œ this๋Š” ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ ์„ ๋œปํ•˜์ง€๋งŒ, JS์—์„  ๋‹ค๋ฅด๋‹ค!
JS์—์„œ this๋Š” ์š”์ˆ ๋žจํ”„ ์ง€๋‹ˆ๋‹ค!! ๋ˆ„๊ฐ€ ํ˜ธ์ถœํ–ˆ๋ƒ์— ๋”ฐ๋ผ ๋ˆ„๊ตฌ์˜ ์ง€๋‹ˆ์ธ์ง€ ๋™์ ์œผ๋กœ ๋‹ฌ๋ผ์ง„๋‹ค.

This

1. ๋ธŒ๋ผ์šฐ์ € ์ „์—ญ์Šค์ฝ”ํ”„์—์„œ console.log(this)

result : window ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด

reason : ๋ธŒ๋ผ์šฐ์ € ์ „์—ญ ๊ฐ์ฒด๊ฐ€ window์ด๊ธฐ ๋•Œ๋ฌธ์—


2. function(){} ๋‚ด๋ถ€์—์„œ this ํ˜ธ์ถœ 

result : window ๊ฐ์ฒด๊ฐ€ ๋‚˜์˜ด

reason : 'ํ•จ์ˆ˜'๋Š” ์„ ์–ธ ์‹œ window ๊ฐ์ฒด์— '๋“ฑ๋ก'๋จ. ์ด ํ•จ์ˆ˜๋Š” class์— ์†ํ•œ ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ , ์ „์—ญ ์Šค์ฝ”ํ”„์—์„œ ํ˜ธ์ถœ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— this๊ฐ€ ์†ํ•œ ๊ณณ์€ window ๊ฐ์ฒด์ž„. ๋”ฐ๋ผ์„œ, window.simpleFun()๋กœ๋„ ํ˜ธ์ถœ๊ฐ€๋Šฅ!

 


3. Class {} ๋‚ด๋ถ€์—์„œ method(){this ํ˜ธ์ถœ} ์ƒ์„ฑ 

  • const counter = new Counter() //์ธ์Šคํ„ด์Šค ์ƒ์„ฑ!
  • '์ „์—ญ์Šค์ฝ”ํ”„'์—์„œ counter.increase() //์ธ์Šคํ„ด์Šค.๋ฉ”์„œ๋“œ ํ˜ธ์ถœ!!

result : this๋Š” ์ธ์Šคํ„ด์Šค counter์„ ๊ฐ€๋ฆฌํ‚ด

์‚ฌ์ง„ ์ƒ์œผ๋กœ, class Counter๊ฐ€ console์— ์ฐํ˜€์„œ ํด๋ž˜์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ, ์ด this๋Š” instance counter์ด๋‹ค

  • reason: ์ธ์Šคํ„ด์Šค counter์— ์ƒˆ๋กœ์šด method๋ฅผ ์ถ”๊ฐ€ ํ›„, counter.increase() ํ˜ธ์ถœํ•ด๋ณด๋ฉด ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๊นŒ์ง€ ์ถ”๊ฐ€๋œ ์ธ์Šคํ„ด์Šค counter๊ฐ€ console์— ์ฐํž™๋‹ˆ๋‹ค.
๐Ÿšซ ์ด ๋•Œ, ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ณ€์ˆ˜์— ๋ฉ”์„œ๋“œ๋ฅผ ํ• ๋‹นํ•˜๋ฉด?!
const caller = counter.increase;
caller() //undefined ๋ฆฌํ„ด๋จโ€‹

const / let์œผ๋กœ ์„ ์–ธ๋˜๋ฉด window ๊ฐ์ฒด์— ๋“ฑ๋ก ์กฐ์ฐจ ์•ˆ๋œ๋‹ค. (์˜ˆ์™ธ. var์€ ๊ฐ€๋Šฅํ•ด์ง..๊ทธ๋ž˜์„œ var์€ ๋น„์ถ”๋น„์ถ”๐Ÿ‘Ž)
function(){console.log(this)}๋ฅผ ๋‹ด์€ caller๋ฅผ caller()๋กœ ํ˜ธ์ถœํ•ด๋ดค์ž, caller์€ ์†ํ•œ ๊ณณ๋„ ์—†๊ณ , window์— ๋“ฑ๋ก๋„ ์•ˆ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฒŒ ์—†๊ณ , undefined๋กœ ๋– ๋ฒ„๋ฆผ.


4. this๊ฐ€ ๋ˆ„๊ฐ€ ํ˜ธ์ถœํ•˜๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฃผ์ธ์˜ ์ง€๋‹ˆ๋ผ๋Š”๊ฑธ ๋ณด๊ณ ์‹ถ๋Šฌ?

์ƒˆ๋กœ์šด class๋ฅผ ์ƒ์„ฑํ›„, ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ ๋‚˜์„œ ๋‹ค๋ฅธ class์—์„œ ์ƒ์„ฑํ–ˆ๋˜ method๋ฅผ ํ• ๋‹น ํ›„ ํ˜ธ์ถœํ•ด๋ณด๋ฉด

this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์ด Counter์ด ์•„๋‹Œ Bob์œผ๋กœ ๋ฐ”๋€Œ์–ด์žˆ๋‹ค!!


์ด์ฒ˜๋Ÿผ, JS๋Š” ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ๊ณณ์œผ๋กœ ํ• ๋‹นํ•˜๋Š” ์ˆœ๊ฐ„ this๋ผ๋Š” ์ •๋ณด๋ฅผ ์žƒ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
object์™€ ํ•จ์ˆ˜์˜ ๊ด€๊ณ„๋ฅผ this๋ฅผ ๊ฝ๊ฝ ๋ฌถ์–ด์ฃผ๋ ค๋ฉด?! bind๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

1. bind ์ด์šฉ

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

const counter = new Counter();
counter.increase();

// const caller= counter.increase; ๋Œ€์‹ ์—!!
const caller = counter.increase.bind(counter); //bind์ด์šฉ!!
caller();

2. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์ด์šฉ

class Counter {
    count = 0;
    increase = () => { //ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์ด์šฉํ•˜๋ฉด bind๋Œ€์‹  ์ด์šฉ ๊ฐ€๋Šฅ
        console.log(this);
    };
}

const counter = new Counter();
counter.increase();

const caller = counter.increase;
// const caller = counter.increase.bind(counter);
caller();

 

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

[Inside JavaScript] Chapter 1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ณธ ๊ฐœ์š”  (0) 2023.03.27
Module  (2) 2023.02.28
Prototype  (5) 2023.02.28
JavaScript Event Loop๋ž€?  (4) 2023.02.22
priceFormatter  (0) 2023.02.08
Comments