当前位置:网站首页>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
边栏推荐
- ArrayList 扩容详解,扩容原理[通俗易懂]
- QT capture interface is displayed as picture or label
- Redis installation and setting up SSDB master-slave environment under Ubuntu 14.04
- 【LeetCode】16、最接近的三数之和
- 保证生产安全!广州要求危化品企业“不安全不生产、不变通”
- qt捕获界面为图片或label显示
- Digital transformation: data visualization enables sales management
- Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
- Zabbix API与PHP的配置
- Problem note - Oracle 11g uninstall
猜你喜欢

Basic operations of SQL database

JVM second conversation -- JVM memory model and garbage collection

cmake 基本使用过程

微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
![[leetcode] 16. The sum of the nearest three numbers](/img/60/6a68333d6e543c601e6ed586b830d0.png)
[leetcode] 16. The sum of the nearest three numbers

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

The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores

Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界

【LeetCode】16、最接近的三数之和

问题随记 —— Oracle 11g 卸载
随机推荐
The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores
TypeScript:const
opencv学习笔记六--图像拼接
solidty-基础篇-结构体和数组,私有 / 公共函数,函数的返回值和修饰符,事件
Zabbix API与PHP的配置
Buuctf reinforcement question ezsql
Redis安装及Ubuntu 14.04下搭建ssdb主从环境
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
What are the books that have greatly improved the thinking and ability of programming?
solidty-基础篇-基础语法和定义函数
Redis installation and setting up SSDB master-slave environment under Ubuntu 14.04
Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
Minimum spanning tree and bipartite graph in graph theory (acwing template)
Rearrangement of overloaded operators
What are the requirements for NPDP product manager international certification registration?
JVM second conversation -- JVM memory model and garbage collection
In hot summer, please put away this safe gas use guide!
241. 为运算表达式设计优先级
DirectX repair tool v4.1 public beta! [easy to understand]