2
2
3
3
> Javascript에서 this는 무엇인가?
4
4
5
+ [ Table of Contents]
6
+
7
+ - [ 상황에 따른 this] ( #상황에-따른-this )
8
+ - [ global scope] ( #global-scope )
9
+ - [ Object's method] ( #objects-method )
10
+ - [ call, apply, bind] ( #call-apply-bind )
11
+ - [ arrow function] ( #arrow-function )
12
+ - [ new] ( #new )
13
+ - [ callback (서브루틴)] ( #callback-서브루틴 )
14
+ - [ 실전 문제들] ( #실전-문제들 )
15
+ - [ Variable vs property] ( #variable-vs-property )
16
+ - [ Cat name] ( #cat-name )
17
+ - [ Delayed greeting] ( #delayed-greeting )
18
+ - [ Artificial method] ( #artificial-method )
19
+ - [ Greeting and farewell] ( #greeting-and-farewell )
20
+ - [ Tricky length] ( #tricky-length )
21
+ - [ Calling arguments] ( #calling-arguments )
22
+
23
+ ---
24
+
5
25
this가 무엇인지 대답하라고 할 때 보통 상황마다 다른 this를 대답하곤 한다.
6
26
7
27
"그래서 this가 뭔데요?"
@@ -10,9 +30,7 @@ this가 무엇인지 대답하라고 할 때 보통 상황마다 다른 this를
10
30
11
31
실행문맥이란 말은 호출자가 누구인지와 같다.
12
32
13
- 콘텍스트(context) 객체는 this가 바라보고 있는 어떤 객체이다.
14
-
15
- 그렇다면 역으로 this는 context를 가리키는 객체이다.
33
+ 콘텍스트(context) 객체는 this가 바라보고 있는 어떤 객체이며 반대로 표현하면 this는 context를 가리키는 것이다.
16
34
17
35
this가 변하는 상황을 먼저 나열해보자
18
36
@@ -27,7 +45,9 @@ this가 변하는 상황을 먼저 나열해보자
27
45
28
46
---
29
47
30
- ## global scope
48
+ ## 상황에 따른 this
49
+
50
+ ### global scope
31
51
32
52
전역공간에서의 this는 전역객체를 가리킨다.
33
53
@@ -42,7 +62,7 @@ var a = 1;
42
62
this .a ; // 1
43
63
```
44
64
45
- ## Object's method
65
+ ### Object's method
46
66
47
67
함수는 자체로 독립적인 기능을 수행
48
68
@@ -86,7 +106,7 @@ testObj.outerFunc();
86
106
87
107
outerFunc 내부의 innerFunc에서 this는 전역 객체가 된다. 이는 callback에서 더 자세히 다룬다.
88
108
89
- ## call, apply, bind
109
+ ### call, apply, bind
90
110
91
111
함수에 명시적으로 this를 바인딩 할 수 있다.
92
112
@@ -107,7 +127,7 @@ bind의 경우는 this 바인딩만 해주고 실행은 직접 해줘야 한다.
107
127
func .bind (this , ` param1` , ` param2` );
108
128
```
109
129
110
- ## arrow function
130
+ ### arrow function
111
131
112
132
화살표 함수는 함수를 선언할 때 this에 바인딩할 객체가 정적으로 결정되며, 화살표 함수를 둘러싸는 렉시컬 스코프(lexical scope)의 this가 사용된다.
113
133
@@ -146,7 +166,7 @@ arrowObj.func(); // outer function
146
166
funcObj .func (); // object function
147
167
```
148
168
149
- ## new
169
+ ### new
150
170
151
171
생성자 함수에서는 인스턴스 자신이된다. (class와 비슷함)
152
172
@@ -159,7 +179,7 @@ var Obj = function (name, age) {
159
179
var obj = new Obj (" 나비" , 1 ); // {name:"나비", age:1}
160
180
```
161
181
162
- ## callback (서브루틴)
182
+ ### callback (서브루틴)
163
183
164
184
콜백함수의 제어권을 가지는 함수(메서드)마다 다르다
165
185
0 commit comments