当前位置:网站首页>The difference between arrow function and ordinary function in JS
The difference between arrow function and ordinary function in JS
2022-07-01 15:01:00 【A small train, besides】
JS The difference between arrow function and ordinary function
The printed results are different
var person = {
say: function () {
},
say2: () => {
}
}
console.dir(person.say)
console.dir(person.say2)

- Arrow function is not
prototypeandargumentsAttribute
this The direction of is different
- Of a normal function
thisIt is determined when the function is running , Basically meet who calls to whom , Except in special cases - Arrowhead function
thisIt is determined in the function definition , Because the arrow function doesn't havethis, So the innerthisIt points to the scope of the upper layer of the arrow function , And cannot passcall, apply, bindchangethisPoint to
var aaaa = ' Big train '
var person = {
aaaa: ' puddle jumper ',
say: function () {
console.log(this.aaaa)
},
say2: () => {
console.log(this.aaaa)
}
}
person.say() // puddle jumper
person.say2() // Big train
Ordinary function say Medium this Because it was person Object call , therefore this It points to person object , The result of printing is puddle jumper
Arrow function say2 Medium this It is determined when the function is defined , And objects do not constitute independent scopes , therefore this It points to the top-level scope window, The result of printing is Big train
Arrow function cannot be a constructor
function Person() {
}
const per = new Person()
Use new Four steps to create a constructor
- Create an empty object
- Link to prototype
- change
thisPoint to - Return new object
// 1. Create an empty object
let obj = {
}
// 2. Assign the object prototype of the object to Person Prototype object
obj.__proto__ = Person.prototype
// 3. change this Point to
let result = Person.call(obj, ...arguments)
// 4. Return new object
return result
Because the arrow function doesn't have prototype Of a prototype object , So if you use new You're going to report a mistake
Arrow function does not have its own arguments
const aa = ()=> {
console.log(arguments)
}
aa()

Because the arrow function here looks up the upper scope arguments, But the global scope window It's not arguments Attribute , So wrong reporting
function aa() {
const bb = ()=> {
console.log(arguments)
}
bb()
}
aa() // Arguments [callee: ƒ, Symbol(Symbol.iterator): ƒ]

The arrow function here will find the upper scope aa Functional arguments
边栏推荐
- opencv学习笔记六--图像特征[harris+SIFT]+特征匹配
- cmake 基本使用过程
- Zabbix API与PHP的配置
- Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
- MySQL 服务正在启动 MySQL 服务无法启动解决途径
- 【LeetCode】16、最接近的三数之和
- 网速、宽带、带宽、流量三者之间的关系是什么?
- The markdown editor uses basic syntax
- Minimum spanning tree and bipartite graph in graph theory (acwing template)
- 保证生产安全!广州要求危化品企业“不安全不生产、不变通”
猜你喜欢

JS中箭头函数和普通函数的区别

It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!

Take you to API development by hand

Opencv Learning Notes 6 -- image mosaic
![[leetcode] 16. The sum of the nearest three numbers](/img/60/6a68333d6e543c601e6ed586b830d0.png)
[leetcode] 16. The sum of the nearest three numbers

Semiconductor foundation of binary realization principle

如何实现时钟信号分频?
![[zero basic IOT pwn] reproduce Netgear wnap320 rce](/img/f7/d683df1d4b1b032164a529d3d94615.png)
[zero basic IOT pwn] reproduce Netgear wnap320 rce

写在Doris毕业后的第一天

炎炎夏日,这份安全用气指南请街坊们收好!
随机推荐
职场太老实,总被欺负怎么办?
opencv学习笔记四--银行卡号识别
Solid basic basic grammar and definition function
Official announcement: Apache Doris graduated successfully and became the top project of ASF!
常见健身器材EN ISO 20957认证标准有哪些
The first technology podcast month will be broadcast soon
Zabbix API与PHP的配置
Written on the first day after Doris graduated
NPDP产品经理国际认证报名有什么要求?
炎炎夏日,这份安全用气指南请街坊们收好!
OpenSSL client programming: SSL session failure caused by an insignificant function
Generate random numbers (4-bit, 6-bit)
opencv学习笔记六--图像特征[harris+SIFT]+特征匹配
Error-tf.function-decorated function tried to create variables on non-first call
Configuration of ZABBIX API and PHP
【天线】【3】CST一些快捷键
These three online PS tools should be tried
JS中箭头函数和普通函数的区别
Rearrangement of overloaded operators
Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching