javascript的箭头函数与传统函数有何不同?【教程】
技术百科
夜晨
发布时间:2026-01-28
浏览: 次 箭头函数没有自己的this、arguments、super或new.target,不能用作构造函数;其this继承自外层词法作用域,不动态绑定;无arguments对象,不支持yield、prototype及caller/callee。
箭头函数没有自己的 this、argum、
super 或 new.target,也不能用作构造函数——这是最根本的区别,不是语法糖那么简单。
箭头函数的 this 永远继承自外层作用域
传统函数的 this 在调用时动态绑定(取决于怎么被调用),而箭头函数根本不绑定 this,它直接从定义时的词法作用域“捕获”外层的 this 值。
常见错误现象:
在对象方法中用箭头函数写回调,结果 this 指向意外(比如指向全局或 undefined):
const obj = {
name: 'test',
regular() {
setTimeout(function() {
console.log(this.name); // undefined(非严格模式下是 global)
}, 100);
},
arrow() {
setTimeout(() => {
console.log(this.name); // 'test' —— 因为箭头函数继承了 arrow() 执行时的 this
}, 100);
}
};
使用场景:
– 事件监听器、定时器、Promise 回调中避免 bind(this) 或缓存 const self = this
– React 类组件的事件处理函数(但注意:现代 React 更推荐函数组件 + useCallback)
箭头函数不能用 new 调用,也没有 prototype
尝试用 new 调用箭头函数会直接抛出 TypeError: xxx is not a constructor。
原因:
– 箭头函数内部没有 [[Construct]] 内部方法
– Function.prototype 上的方法(如 call、apply、bind)对它无效(虽然能调用,但不会改变 this)
– arrowFn.prototype 是 undefined
所以这些写法都错:
– new (() => {})
– (() => {}).prototype
– (() => {}).bind(obj)(虽不报错,但无实际效果)
箭头函数没有 arguments 对象,也不支持 yield
在箭头函数体内访问 arguments,得到的是外层函数的 arguments(如果外层也没有,就报 ReferenceError)。
替代方案:
– 用剩余参数 ...args(推荐,更语义化)
– 或显式命名参数(适合固定参数个数)
其他限制:
– 不能用作 Generator 函数(不支持 function* 语法,也不支持 yield)
– 没有 caller 和 callee 属性(已被废弃,但传统函数仍可访问)
– 不能用 yield,哪怕嵌套在 generator 内部也不行
什么时候不该用箭头函数
以下情况必须用传统函数:
– 定义对象方法(尤其是需要正确 this 指向当前对象时)
– 需要 arguments 对象且无法改用剩余参数时(极少见)
– 显式构造实例(new MyClass())
– Vue 选项对象中的 methods、computed、生命周期钩子(Vue 2/3 都依赖 this 绑定)
– 作为事件监听器并需通过 event.currentTarget 或 this 访问 DOM 元素(除非你手动传参)
容易被忽略的一点:
箭头函数的“简洁”可能掩盖执行上下文的丢失——尤其在团队协作或维护老代码时,看到 () => {} 很容易默认它“和普通函数一样”,结果调试半天才发现 this 不是想要的那个。
# 的是
# 自己的
# 这是
# 也不
# 尤其是
# 绑定
# 什么时候
# app
# 不支持
# 对象
# javascript
# java
# 区别
# 构造函数
# function
# 事件
# Event
# this
# 回调
# 继承
# 作用域
# dom
# const
# 不能用
# promise
# undefined
# constructor
# react
# vue
# prototype
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- c++怎么用jemalloc c++替换默认内存分
- c# F# 的 MailboxProcessor
- Win11任务栏天气怎么关闭 Win11隐藏天气小
- c++中如何计算坐标系中两点间距离_c++勾股定理
- c++中的可变参数模板(variadic temp
- C++如何编写函数模板?(泛型编程入门)
- Windows10系统服务优化指南_Win10禁用
- Windows怎样关闭开始菜单推荐广告_Windo
- 如何使用Golang反射将map转换为struct
- win11 OneDrive怎么彻底关闭 Win1
- PHP主流架构怎么监控运行状态_工具推荐【操作】
- Python与MongoDB NoSQL开发实战_
- php错误怎么开启_display_errors与
- Win11怎么关闭右下角弹窗_Win11拦截系统通
- 如何在 PHP 单元测试中正确模拟带方法的图像处理
- Windows10如何更改开机密码_Win10登录
- Win11怎么设置任务栏大小_Windows11注
- Linux如何安装JDK11_Linux环境变量配
- Win11怎么硬盘分区 Win11新建磁盘分区详细
- Win10如何更改开机密码_Windows10登录
- c++的mutex和lock_guard如何使用
- Win11怎么关闭自动维护 Win11禁用系统自动
- 如何在Golang中处理JSON字段缺失_Gola
- php查询数据怎么分组_groupby分组查询配合
- Go 语言标准库为何不提供泛型 Contains
- Win11触摸板没反应怎么办_开启Win11笔记本
- 如何提升Golang程序I/O性能_Golang
- 如何在 Go 中创建包含 map 的 slice(
- Win11怎么更改管理员名字 Win11修改账户名
- 如何在 Go 中判断变量是否为函数类型
- Windows蓝屏错误0x0000002C怎么解决
- Win11关机快捷键是什么_Win11快速关机方法
- 如何在 Laravel 中通过嵌套关联关系进行 o
- Windows10电脑怎么设置防火墙出站规则_Wi
- Win11快速助手怎么用_Win11远程协助连接教
- php订单日志怎么记录评价_php记录订单评价日志
- Win10怎么关闭自动更新错误重启 Win10策略
- Win10怎样卸载iTunes_Win10卸载iT
- Win11怎么查看电脑配置_Win11硬件配置详细
- c# await 一个已经完成的Task会发生什么
- Win11无法识别耳机怎么办_解决Win11插耳机
- 如何解决同一段404代码在不同主机上表现不一致的问
- php打包exe怎么传递参数_命令行参数接收方法【
- php打包exe后无法写入文件_权限问题解决方法【
- Windows10怎么查看系统激活状态_Windo
- Mac如何查看电池健康百分比_Mac系统信息电源检
- Windows如何使用注册表查找和删除项?(reg
- Python大文件处理策略_内存优化说明【指导】
- Mac的“预览”如何合并多个PDF_Mac文件处理
- c++获取当前时间戳_c++ time函数使用详解

QQ客服