箭头函数是ES6引入的简洁函数语法,无独立this、arguments、prototype,不能作构造函数,不绑定动态this,适合简短回调;有隐式返回和剩余参数替代arguments。

箭头函数是 ES6 引入的一种简洁的函数定义语法,它没有自己的 this、arguments、super 或 new.target,而是继承自外层作用域。它更轻量,适合写简短的回调或逻辑表达式,但不适用于需要动态 this 的场景(比如对象方法、构造函数、事件监听器等)。
箭头函数没有独立的 this
普通函数调用时,this 取决于调用方式(如对象调用、call/apply、事件触发等);而箭头函数的 this 始终绑定定义时所在上下文的 this,无法被改变。
例如:
const obj = {
name: 'Alice',
regular() { console.log(this.name); }, // 输出 'Alice'
arrow: () => { console.log(this.name); } // 输出 undefined(this 指向全局或模块顶层)
};
obj.regular(); // 'Alice'
obj.arrow(); // undefined(非严格模式下可能是 globalThis)
不能作为构造函数使用
箭头函数没有 prototype 属性,也没有 [[Construct]] 内部方法,因此不能用 new 调用,否则会报错。
立即学习“Java免费学习笔记(深入)”;
-
const fn = () => {};→new fn()抛出TypeError: fn is not a constructor - 普通函数:
function Fn() {}→new Fn()正常创建实例
没有 arguments 对象
箭头函数内部访问 arguments 会沿作用域链向上查找,实际获取的是外层函数的 arguments(如果存在),否则为 undefined。推荐用剩余参数 ...args 替代。
function outer() {
const arrow = () => console.log(arguments[0]);
arrow('hello'); // 输出 'hello'(取自 outer 的 arguments)
}
outer('hello');
语法更简洁,隐式返回
当函数体只有一条表达式且无大括号时,箭头函数自动返回该表达式结果,无需 return 关键字。
-
x => x * 2等价于function(x) { return x * 2; } -
(a, b) => a + b→ 多参数需括号,单参数可省略 -
() => 'hi'→ 无参必须写空括号











