Lesson 12 of 55
javascriptThe `this` Keyword – Binding Rules
12 mins
Course progress22%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 12 of 55
javascript12 mins
Keep these ideas in mind as you work through the lesson.
How this is set depends on call site:
this is undefined in strict mode, otherwise the global object.obj.method() → this is obj.new) – this is the newly created object.this is lexically inherited from the surrounding scope.this points to the element the listener is attached to (unless an arrow function is used).const btn = document.querySelector('button');
btn.addEventListener('click', function(){ console.log(this); }); // logs <button>
btn.addEventListener('click', () => { console.log(this); }); // logs Window (lexical this)