Chapter XXI · The `this` Keyword
What is `this`?
The `this` keyword is a special identifier reference that points to the object executing the current function context.
01 How It Works
Think of a pronoun like 'I' or 'me' whose meaning changes depending on who is speaking.
`this` is not statically bound; its value is determined dynamically based on *how* a function is invoked.
example.js
const obj = {
name: "Alice",
show() { console.log(this.name); }
};
obj.show(); // "Alice"
1. Identify the invocation context of a function.
2. Resolve `this` binding based on execution caller rules.
3. Access object state properties.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
function printThis() {
console.log(this);
}
printThis(); // Global object or undefined in strict mode
Key Takeaway
`this` refers to the execution context owner object, determined entirely by how a function is called.