Key Concepts in JavaScript
this
1.”this” is the object that calls the method.
1 | let cat = { |
The output:My name is Tom
2.Global functions are essentially methods of the window.
1 | function func(){ |
The output:[object Window]
3.dom element
1 | <!DOCTYPE html> |
The output is:<button id="btn">Click me</button>
4.However, if we write like this, the output is surprisingly changed.
1 | <!DOCTYPE html> |
The output is:[object Window]
Why this happened?
Regular Function function() {} has dynamic this binding, while Arrow Function () => {} has lexical this binding.
For Regular Function: this refers to the element that triggered the event.
For Arrow Function: this is determined by where the function is defined, not how it’s called.
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
