箭头函数是ES6引入的简洁函数语法,具有词法this绑定、无arguments对象、不可用new调用等特性。它省略function关键字及return(单表达式时),但无法作为构造函数,且this指向定义时外层普通函数的this。

箭头函数是 ES6 引入的一种简洁的函数定义方式,用 => 符号表示。它不只是写法更短,更关键的是在 this 绑定、arguments、new 调用 等行为上和普通函数有本质区别。
语法更简洁,省略 function 关键字和 return(单表达式时)
箭头函数允许省略 function 关键字、括号(单参数时)、花括号和 return(单表达式返回值时)。
例如:
// 普通函数
const add = function(a, b) { return a + b; };
// 箭头函数(等价写法)
const add = (a, b) => a + b;
const square = x => x * x; // 单参数可省括号
const log = () => console.log('hi'); // 无参必须写空括号
this 指向是词法绑定,不动态变化
普通函数的 this 取决于调用方式(谁调用,this 就是谁),而箭头函数没有自己的 this,它会沿作用域链向上找外层第一个普通函数的 this 值 —— 也就是“定义时”所在上下文的 this。
立即学习“Java免费学习笔记(深入)”;
常见影响场景:
- 对象方法中使用箭头函数,
this不指向该对象,而是外层作用域(比如全局或构造函数) - 事件回调、定时器、Promise 回调中,用箭头函数可避免手动
bind(this)或缓存const self = this
没有 arguments、caller、callee
箭头函数内部访问不到 arguments 对象。如果需要类数组参数,应使用剩余参数(...args)代替。
例如:
function oldFn() {
console.log(arguments); // [1, 2, 3]
}
const newFn = () => {
console.log(arguments); // ReferenceError: arguments is not defined
};
const newFn2 = (...args) => {
console.log(args); // [1, 2, 3]
};
不能作为构造函数,不能用 new 调用
箭头函数没有 [[Construct]] 内部方法,因此不能用 new 实例化,否则会报错 TypeError: xxx is not a constructor。
它也没有 prototype 属性,无法用于实现原型继承。
所以:需要创建实例对象、依赖 this 指向实例、或需原型链的场景,必须用普通函数(包括 function 声明、表达式或 class 方法)。











