How does the “this” keyword work in different contexts?
Context this Refers To
Follow me on LinkedIn:
Global window or global
Method The object owning the method
Constructor The new instance
Arrow function Lexical (surrounding) scope
Example:
const obj = {
value: 10,
show: function() { console.log(this.value); }
obj.show(); // 10