箭头函数无this、无arguments、不能new调用、返回对象需括号包裹;this沿作用域链继承,arguments需用...args替代,无prototype故不可构造,返回对象字面量必须用()包裹。
this,这点
必须立刻意识到箭头函数不绑定 this,它会沿作用域链向上找最近的非箭头函数的 this。这意味着在对象方法、事件回调或定时器中直接用箭头函数,this 很可能不是你预期的对象实例。
this 由调用方式决定(如 obj.fn() 中 this 是 obj)this 固定为定义时所在词法作用域的 this,无法通过 call/apply/bind 修改onClick={() => this.handleClick()} 没问题,但若写成 const handler = () => { console.log(this); } 再传给子组件,this 就可能丢失上下文arguments 在箭头函数里根本不存在箭头函数没有自己的 arguments 对象。试图访问它会报 ReferenceError: arguments is not defined。
...args —— 它在箭头函数中完全可用() => { console.log(arguments); }(...args) => { console.log(args); }arguments
new 会直接报错箭头函数没有 prototype 属性,也没有 [[Construct]] 内部方法,所以不能被 new 调用。
new (() => {}) 会抛出 TypeError: xxx is not a constructor
class
super 或 new.target
箭头函数单表达式隐式返回时,如果想返回一个对象,必须用小括号包裹,否则大括号会被解析为函数体。
undefined):const fn = () => { a: 1 }; —— 这里 { a: 1 } 被当成代码块,不是对象const fn = () => ({ a: 1 });const arr = () => [1, 2];是安全的
this、无 arguments、无 prototype”是设计使然,不是缺陷。用错地方时,问题往往不报错,而是静默失效——比如 this 指向意外的全局对象,这种 bug 最难排查。