Functions
The this keyword
How this is determined by how a function is called.
this is not fixed at definition time — it is set by how a function is called (its call site).
`this` is like the word "here" — its meaning depends entirely on where it is spoken, not where it was written down.
Key concepts
1
The four binding rules, in priority order: new binding (a constructor call), explicit binding (call/apply/bind), implicit binding (called as obj.method(), so this is obj), and default binding (undefined in strict mode, otherwise the global object).
newexplicitimplicitdefaultcall
2
Arrow functions ignore all of that — they capture this lexically from the enclosing scope, which is exactly why they are handy for callbacks inside methods.
Arrow functionsthis
javascript
const user = {
name: 'Ada',
greet() { return `Hi, ${this.name}`; },
greetLater() {
setTimeout(() => console.log(this.name), 0); // arrow keeps this = user
}
};
user.greet(); // 'Hi, Ada'
const fn = user.greet;
// fn(); // this is undefined (strict) — lost binding
fn.call(user); // 'Hi, Ada'