> For the complete documentation index, see [llms.txt](https://leeans-dev-book.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leeans-dev-book.gitbook.io/docs/lecture/es6-js/this.md).

# this

1. 기본은 window

아무 html 파일에서 \<script> 태그 열고&#x20;

console.log(this) 찍으면 window 나옴

```html
index.html

<html>
    <script>
        console.log(this); //window
    </script>
</html>
```

마찬가지로 함수안에서도 window

```javascript
function test() {
    console.log(this); // window
}
```

Q. Window?&#x20;

window는 모든 전역변수, 함수 , DOM 을 보관하고 관리하는 전역객체

전체를 감싸는 object 라고 생각하면 편함

2. object 내에 함수에서는 부모 object

```javascript
var obj1 = {
    data : 'data',
    fun1 : function() {console.log(this)}
}


obj1.fun1(); //obj1이 출력된다.
```

object 내에서의 this는 부모 object를 가르킨다

여기선 obj1이 출력됨

```javascript
var obj2 = {
    data : {
        fun2 : function() {console.log(this)}  
    }
}

obj2.data.fun2(); // this는obj2.data
```

사실 1번과 2번은 동일한 이야기

```html
<script>
    function fun1() {
        console.log(this);
    }
    
    window.fun1 = function() {
        console.log(this)
    }
    //위 코드는 둘다 같은 내용임
</script>
```

우리가 아무 선언없이 함수나 변수 만들면 그건 전부 window 객체에 담긴다.

3. constructor 안에서 쓰면 constructor로 새로 생성되는 object

```javascript
function fun3() {
    this.name = 'Lee';
}
```

여기서 this는 fun3으로부터 새로 생성될 object를 의미함

```javascript
var obj3 = new fun3();

console.log(obj3.name); //'Lee'가 출력됨
```

1. eventlistener 안에서 쓰면 e.currentTarget
