当前位置:网站首页>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
边栏推荐
- 241. 为运算表达式设计优先级
- Solid basic structure and array, private / public function, return value and modifier of function, event
- ArrayList 扩容详解,扩容原理[通俗易懂]
- 项目中字符串判空总结
- Opencv interpolation mode
- Markdown编辑器使用基本语法
- Mongodb second talk - - mongodb High available Cluster Implementation
- OpenSSL client programming: SSL session failure caused by an insignificant function
- 对于编程思想和能力有重大提升的书有哪些?
- What data capabilities do data product managers need to master?
猜你喜欢
opencv学习笔记六--图像特征[harris+SIFT]+特征匹配
Filter &(登录拦截)
[leetcode 324] swing sorting II thinking + sorting
JVM第二话 -- JVM内存模型以及垃圾回收
Opencv learning note 4 -- bank card number recognition
Rearrangement of overloaded operators
What are the books that have greatly improved the thinking and ability of programming?
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
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
首届技术播客月开播在即
随机推荐
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
手把手带你入门 API 开发
竣达技术丨多台精密空调微信云监控方案
Solidty智能合约开发-简易入门
Pat 1065 a+b and C (64bit) (20 points) (16 points)
Storage form of in-depth analysis data in memory
What value can NPDP bring to product managers? Do you know everything?
深度分析数据在内存中的存储形式
Microservice development steps (Nacos)
What if you are always bullied because you are too honest in the workplace?
Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
Error-tf. function-decorated function tried to create variables on non-first call
期末琐碎知识点再整理
JVM performance tuning and practical basic theory part II
网速、宽带、带宽、流量三者之间的关系是什么?
Buuctf reinforcement question ezsql
opencv学习笔记六--图像拼接
Filter &(登录拦截)
idea中新建的XML文件变成普通文件的解决方法.
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码