对象和类的方法必须用函数表达式或方法简写,以确保this动态绑定;箭头函数仅适用于回调场景且不暴露为可任意调用的方法,因其固化外层this、无arguments、不可new调用。

箭头函数和函数表达式在定义方法时不能混用——选错会直接导致 this 失效、无法继承、调用报错等运行时问题。关键不在“哪个更简洁”,而在“这个方法是否需要动态绑定上下文”。
对象方法必须用函数表达式
对象内部的方法(如 user.getName())依赖调用时的上下文决定 this 指向。函数表达式能动态绑定,箭头函数则固化外层 this(通常是 undefined 或全局对象)。
- ✅ 正确:使用函数表达式或方法简写(ES6)
const user = {
name: 'Alice',
getName: function() { return this.name; }, // 函数表达式
greet() { return `Hello, ${this.name}`; } // 方法简写,本质仍是普通函数
};
- ❌ 错误:箭头函数写法
const user = {
name: 'Alice',
getName: () => this.name // this 指向外层,不是 user
};
user.getName(); // undefined
立即学习“Java免费学习笔记(深入)”;
类中实例方法推荐函数表达式或方法简写
类的原型方法需支持被子类重写、借用(如 Array.prototype.map.call(array, fn)),这要求 this 可被动态绑定。箭头函数会固化定义时的 this,破坏继承链和方法复用能力。
- ✅ 推荐写法(可继承、可借用)
class User {
constructor(name) { this.name = name; }
getName() { return this.name; } // 方法简写
logName: function() { console.log(this.name); } // 函数表达式
}
- ❌ 避免写法(子类无法覆盖,
call/apply失效)
class User {
getName = () => this.name; // 箭头函数绑定构造时的 this,非调用时的 this
}
回调场景优先用箭头函数
当函数作为回调传入(如事件监听、定时器、Promise 链),且逻辑依赖外层作用域的 this 或变量时,箭头函数天然避免手动 bind 或 that = this。
- ✅ 合理使用(无需额外绑定)
class Timer {
constructor() { this.seconds = 0; }
start() {
setInterval(() => {
this.seconds++; // this 正确指向 Timer 实例
console.log(this.seconds);
}, 1000);
}
}
- ⚠️ 注意边界:仅限回调本身不暴露为可被任意调用的方法
需要 arguments 或 new 调用时只能用函数表达式
箭头函数没有 arguments 对象,也不能用 new 实例化。若方法需处理不定参数或设计为构造器(如工厂函数),函数表达式是唯一选择。
- 需要不定参数 → 用函数表达式 +
arguments,或改用剩余参数(...args) - 需要被
new调用 → 必须用函数表达式(或函数声明) - 需兼容老环境或调试堆栈友好 → 函数表达式命名更清晰(如
const validate = function validate() { ... })


















