본문 바로가기

ES6

arrow function을 사용하지 않아야할 때. 'this'

반응형

</head>

    <button>aaa</button>

<body>

    <script>

        const button = document.querySelector('button')

 

        button.addEventListener('click'function(){

            console.log(this)

            this.style.backgroundColor = "red";

        })

    </script>

 

 

this가 버튼을 가르키지만, 애로우 펑션으로 바꾸면 this는 window를 가르키게 된다.

반응형

'ES6' 카테고리의 다른 글

filter() - includes()  (0) 2020.09.14
find() - includes()  (0) 2020.09.14
arrow function  (0) 2020.09.09
block scope  (0) 2020.09.09
dead zone  (0) 2020.09.09