ES6

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

liufeier 2020. 9. 9. 18:29
반응형

</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를 가르키게 된다.

반응형