当前位置:网站首页>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
边栏推荐
- Quelle valeur le pdnp peut - il apporter aux gestionnaires de produits? Vous savez tout?
- [15. Interval consolidation]
- What are the requirements for NPDP product manager international certification registration?
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- C#学习笔记(5)类和继承
- 竣达技术丨多台精密空调微信云监控方案
- MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
- In hot summer, please put away this safe gas use guide!
- opencv学习笔记六--图像拼接
- Configuration of ZABBIX API and PHP
猜你喜欢

C#学习笔记(5)类和继承
![[zero basic IOT pwn] reproduce Netgear wnap320 rce](/img/f7/d683df1d4b1b032164a529d3d94615.png)
[zero basic IOT pwn] reproduce Netgear wnap320 rce
![[14. Interval sum (discretization)]](/img/e5/8b29aca7068a6385e8ce90c2742c37.png)
[14. Interval sum (discretization)]
![[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion](/img/e1/d35e1d382e0e945849010941b219d3.png)
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion

Written on the first day after Doris graduated

leetcode:329. Longest increasing path in matrix

Basic use process of cmake

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

What are the books that have greatly improved the thinking and ability of programming?

OpenSSL client programming: SSL session failure caused by an insignificant function
随机推荐
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
【LeetCode】16、最接近的三数之和
Pat 1121 damn single (25 points) set
智能运维实战:银行业务流程及单笔交易追踪
Cannot link redis when redis is enabled
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
MySQL 服务正在启动 MySQL 服务无法启动解决途径
手把手带你入门 API 开发
[zero basic IOT pwn] reproduce Netgear wnap320 rce
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
opencv学习笔记四--银行卡号识别
idea中新建的XML文件变成普通文件的解决方法.
leetcode:329. 矩阵中的最长递增路径
Basic operation of database
对于编程思想和能力有重大提升的书有哪些?
Tensorflow 2. X realizes iris classification
Basic use process of cmake
IDEA全局搜索快捷键(ctrl+shift+F)失效修复
深度分析数据在内存中的存储形式
Demand prioritization method based on value quantification