当前位置:网站首页>JS中箭头函数和普通函数的区别
JS中箭头函数和普通函数的区别
2022-07-01 14:55:00 【小火车况且况且】
JS中箭头函数和普通函数的区别
打印的结果不同
var person = {
say: function () {
},
say2: () => {
}
}
console.dir(person.say)
console.dir(person.say2)

- 箭头函数是没有
prototype和arguments属性的
this的指向不同
- 普通函数的
this是在函数运行时候确定的, 基本满足谁调用指向谁, 特殊情况除外 - 箭头函数的
this是在函数定义就确定了, 因为箭头函数是没有this, 所以内层的this就指向箭头函数上层的作用域, 并且不可以通过call, apply, bind改变this指向
var aaaa = '大火车'
var person = {
aaaa: '小火车',
say: function () {
console.log(this.aaaa)
},
say2: () => {
console.log(this.aaaa)
}
}
person.say() // 小火车
person.say2() // 大火车
普通函数say中的this因为是被person对象调用, 所以this就指向了person对象, 打印的结果就是小火车
箭头函数say2中的this是在函数定义的时候就确定了, 而且对象不构成独立的作用域, 所以this就指向了顶级作用域window, 打印结果就是大火车
箭头函数不能作为构造函数
function Person() {
}
const per = new Person()
使用new 创建构造函数的四个步骤
- 创建一个空对象
- 链接到原型
- 改变
this指向 - 返回新的对象
// 1.创建一个空对象
let obj = {
}
// 2.将对象的对象原型赋予Person的原型对象
obj.__proto__ = Person.prototype
// 3.改变this指向
let result = Person.call(obj, ...arguments)
// 4. 返回新的对象
return result
因为箭头函数是没有prototype原型对象的, 因此如果直接使用new 就会报错
箭头函数没有自己的arguments
const aa = ()=> {
console.log(arguments)
}
aa()

因为这里的箭头函数往上层作用域查找arguments, 但是全局的作用域window是没有arguments属性的, 所以报错
function aa() {
const bb = ()=> {
console.log(arguments)
}
bb()
}
aa() // Arguments [callee: ƒ, Symbol(Symbol.iterator): ƒ]

这里的箭头函数会找到上层作用域aa函数的arguments
边栏推荐
- Pat 1065 a+b and C (64bit) (20 points) (16 points)
- 【15. 区间合并】
- [stage life summary] I gave up the postgraduate entrance examination and participated in the work. I have successfully graduated and just received my graduation certificate yesterday
- Detailed explanation of ArrayList expansion, expansion principle [easy to understand]
- 项目中字符串判空总结
- [lock] redis lock handles concurrency atomicity
- JVM second conversation -- JVM memory model and garbage collection
- 基于价值量化的需求优先级排序方法
- Microservice development steps (Nacos)
- Redis安装及Ubuntu 14.04下搭建ssdb主从环境
猜你喜欢

官宣:Apache Doris 顺利毕业,成为 ASF 顶级项目!

互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码

Minimum spanning tree and bipartite graph in graph theory (acwing template)

写在Doris毕业后的第一天

One of the first steps to redis

Guess lantern riddles, not programmers still can't understand?

IDEA全局搜索快捷键(ctrl+shift+F)失效修复

Music player development example (can be set up)

JVM第二话 -- JVM内存模型以及垃圾回收

Semiconductor foundation of binary realization principle
随机推荐
关于软件测试的一些思考
leetcode:329. Longest increasing path in matrix
En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié
项目中字符串判空总结
Minimum spanning tree and bipartite graph in graph theory (acwing template)
Opencv Learning Notes 6 -- image mosaic
Guess lantern riddles, not programmers still can't understand?
What if you are always bullied because you are too honest in the workplace?
22-06-26周总结
Detailed explanation of ArrayList expansion, expansion principle [easy to understand]
[zero basic IOT pwn] reproduce Netgear wnap320 rce
竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测
适合没口才的人做,加入中视频伙伴计划收益是真香,一个视频拿3份收益
opencv学习笔记四--银行卡号识别
Zabbix API与PHP的配置
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
These three online PS tools should be tried
Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
【阶段人生总结】放弃考研,参与到工作中,已经顺利毕业了,昨天刚领毕业证
The markdown editor uses basic syntax