this - "์๊ธฐ์ฐธ์กฐ๋ณ์"
์ ์
- ๋ด๊ฐ ์ด๋ป๊ฒ ๋ถ๋ฆฌ๋๋์ ๋ฐ๋ผ ๋ฐ์ธ๋ฉ ๋๋ ๊ฒ.
๋ชฉ์
- "๊ฐ์ฒด"์์ "๋ด"๊ฐ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด
"์ด๋์" "์ด๋ป๊ฒ" ๋ถ๋ฅด๋๋์ ๋ฐ๋ผ this๋ ๋ฌ๋ผ์ง๋ค!
- ์ผ๋ฐ ํจ์๋ก ํธ์ถ ๋์์ ๋ -> **์ ์ญ ๊ฐ์ฒด**
- ํจ์() -> window
function test(){ console.log(this) } test()
Window {0: global, window: Window, self: Window, document: document, name: '', location: Location, โฆ}
- ๋ฉ์๋๋ก ํธ์ถ ๋์์ ๋ -> **"๊ทธ" ๋ฉ์๋๋ฅผ ๋ณด์ ํ ๊ฐ์ฒด **
- ๊ฐ์ฒด.๋ฉ์๋() ->๊ฐ์ฒด
const obj = {} const obj = { test : function(){ console.log(this) } } obj.test()
{test: ฦ}
test: ฦ ()
arguments: null
caller: null
length: 0
name: "test"
prototype: {constructor: ฦ}
[[FunctionLocation]]: VM4936:2
[[Prototype]]: ฦ ()
[[Scopes]]: Scopes[2]
[[Prototype]]: Object
- ์์ฑ์ ํจ์๋ก ํธ์ถ ๋์์ ๋ -> **"๋ฏธ๋"์ ์์ฑ๋ "์ธ์คํด์ค"** -> ๊ฐ์ฒด
Teacher {name: 'iseo'} name: "iseo" [[Prototype]]: Object constructor: ฦ Teacher(name) [[Prototype]]: Object
//cf. const person2 = Teacher('iseo') //์ด๋ ๊ฒ ๋ฃ์ผ๋ฉด(newํค์๋ ๋นผ๋จน์ผ๋ฉด) ์ผ๋ฐ ํจ์ ํธ์ถ๊ณผ ๊ฐ์์ ธ ์ ์ญ์ธ window์ Teacher : 'iseo'๊ฐ ๋ค์ด๊ฐ๋ฒ๋ฆผ.... //๊ฒฐ๋ก : ์ด ๊ฒฝ์ฐ์ this๋ window์ ๋ฐ์ธ๋ฉ ๋์ด๋ฒ๋ฆฐ๋ค!!
- function Teacher(name){ this.name = name //"๋ฏธ๋"์ ์์ฑ๋ "์ธ์คํด์ค" } const person = new Teacher('iseo') person
- apply/call/bind๋ก "๊ฐ์ "ํธ์ถ ๋์์ ๋
|function ํค์๋์ ํ์ดํ ํจ์ ์ฐจ์ด

- this.radiuses๋ ๋ฉ์๋(๊ฐ์ฒด.๋ฉ์๋๋ช ()์ผ๋ก ํธ์ถ ๊ฐ๋ฅ) ์์์ด๊ธฐ ๋๋ฌธ์ ์ ์์ ์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅ
- forEach์ ์ฝ๋ฐฑํจ์์ ๊ฒฝ์ฐ ๋ฉ์๋๊ฐ ์๋ (๊ฐ์ฒด.๋ฉ์๋๋ช ()์ผ๋ก ํธ์ถ ๋ถ๊ฐ๋ฅ)
- ๋๋ฌธ์ ์ฝ๋ฐฑํจ์ ๋ด๋ถ์ this๋ window๊ฐ ๋์ด this.PI๋ ์ ์์ ์ผ๋ก ์ ๊ทผ ๋ถ๊ฐ๋ฅ
- ์ด ์ฝ๋ฐฑํจ์ ๋ด๋ถ์์ this.PI์ ์ ๊ทผํ๊ธฐ ์ํด์ ํจ์๊ฐ์ฒด.bind(this) ๋ฉ์๋๋ฅผ ์ฌ์ฉ.
- ์ด ๋ฒ๊ฑฐ๋ก์ด bind ๊ณผ์ ์ ์์ค ๊ฒ์ด ํ์ดํ ํจ์
Summary
- ํจ์ ๋ด๋ถ์ this ํค์๋๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ
- ํ์ดํ ํจ์์ function ํค์๋๋ก ์ ์ธํ ํจ์๊ฐ ๋ค๋ฅด๊ฒ ๋์
- ํจ์ ๋ด๋ถ์ this ํค์๋๊ฐ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ
- ์์ ํ ๋์ผํ๊ฒ ๋์
์ฐธ๊ณ ํ๊ธฐ ์ข์ ๋ธ๋ก๊ทธ ๊ธ ๋งํฌ
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript - AJAX, Asynchronous (๋น๋๊ธฐ) (0) | 2022.05.03 |
---|---|
JavaScript - ํ๋กํ ํ์ (prototype), ํด๋์ค(class) (0) | 2022.05.03 |
JavaScript - ๊ฐ์ฒด(Objects) (0) | 2022.04.27 |
JavaScript - ๋ฐฐ์ด(Arrays) (0) | 2022.04.27 |
JavaScript - ๋ฌธ์์ด(String) (0) | 2022.04.26 |
๋๊ธ