Arrow function không có this riêng, kế thừa this từ lexical scope nơi nó được định nghĩa.
Khi dùng làm method, this không trỏ đến object chứa method.
javascript
const obj = {
name: 'Alice',
greet: () => this // arrow: this = global/undefined
};
console.log(obj.greet()); // undefined (hoặc Window)
const obj2 = {
name: 'Alice',
greet() { return this.name; } // regular method: OK
};
console.log(obj2.greet()); // 'Alice'Dùng function thông thường cho methods, arrow function cho callbacks bên trong method.
Arrow functions have no own this; they inherit this from the lexical scope where they are defined.
When used as a method, this does not point to the containing object.
javascript
const obj = {
name: 'Alice',
greet: () => this // arrow: this = global/undefined
};
console.log(obj.greet()); // undefined (or Window)
const obj2 = {
name: 'Alice',
greet() { return this.name; } // regular method: OK
};
console.log(obj2.greet()); // 'Alice'Use regular functions for methods, and arrow functions for callbacks inside methods.