JavaScript Scope & Arrow Functions
Key Points
{}in object literals are not scoped- Object literals do not create a new scope
Arrow Functions & this
- Arrow functions use lexical scoping
- They inherit
thisfrom surrounding code (where they were defined) - They do not determine
thisbased on the caller
// Scope: Global / Module (Top Level)
// 'this' = Window (or undefined in strict mode)
const client = {
// <-- NOT a scope barrier!
// Arrow function looks OUTSIDE the object for 'this'
// It inherits from Top Level scope
getContacts: async () => {
console.log(this); // 'this' = Window/undefined
},
};
Remember: Object
{}≠ scope. Arrow functions capturethisfrom where they’re written, not where they’re called.