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

箭头函数是 ES6 引入的一种简洁的函数定义语法,它没有自己的 this、arguments、super 或 new.target,而是继承自外层作用域。它更轻量,适合写简短的回调或逻辑表达式,但不适用于需要动态 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(如果存在),否则为 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' → 无参必须写空括号以上就是javascript中箭头函数是什么_它与普通函数有什么区别?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号