this

this

this๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋งฅ๋ฝ(context)๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋งฅ๋ฝ์ด๋ผ๋Š” ๊ฒƒ์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ ์ฆ‰ ํ•จ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœํ•˜๋А๋ƒ์— ๋”ฐ๋ผ์„œ this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ๋œป์ด๋‹ค.

ํ•จ์ˆ˜ํ˜ธ์ถœ

function func(){
    if(window === this){
        document.write("window === this");
    }
}
func(); //window === this

๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ

var o = {
    func : function(){ //๋ฉ”์†Œ
        if(o === this){
            document.write("o === this");
        }
    }
}
o.func(); //o === this

๊ฐ์ฒด์˜ ์†Œ์†์ธ ๋ฉ”์†Œ๋“œ์˜ this๋Š” ๊ทธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.

์ƒ์„ฑ์ž์˜ ํ˜ธ์ถœ

apply, call

ํ•จ์ˆ˜์˜ ๋ฉ”์†Œ๋“œ์ธ apply, call์„ ์ด์šฉํ•˜๋ฉด this์˜ ๊ฐ’์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฉ”์†Œ๋“œ(๋…ธ์˜ˆ) ๋Š” ๊ฐ์ฒด(์ฃผ์ธ) ์— ์ข…์†๋˜์–ด ์žˆ๋‹ค. apply -> ์ด๋Ÿฌํ•œ๊ฒƒ์„ ์ œ์–ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

Last updated

Was this helpful?