当前位置:网站首页>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
边栏推荐
- Basic operation of database
- DirectX修复工具V4.1公测![通俗易懂]
- tensorflow2-savedmodel convert to pb(frozen_graph)
- Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
- [Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
- The solution to turn the newly created XML file into a common file in idea
- 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
- 网速、宽带、带宽、流量三者之间的关系是什么?
- 基于价值量化的需求优先级排序方法
- Opencv Learning Notes 6 -- image mosaic
猜你喜欢

MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology

Word2vec yyds dry goods inventory

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

写在Doris毕业后的第一天
![[15. Interval consolidation]](/img/6c/afc46a0e0d14127d2c234ed9a9d03b.png)
[15. Interval consolidation]
![[getting started with Django] 13 page Association MySQL](/img/78/cbf88ae3c3d311edd7d9af8c985749.jpg)
[getting started with Django] 13 page Association MySQL "multi" field table (check)

Opencv Learning Notes 6 -- image mosaic

Microservice development steps (Nacos)

The first word of JVM -- detailed introduction to JVM and analysis of runtime data area

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
随机推荐
Mongodb second talk - - mongodb High available Cluster Implementation
C learning notes (5) class and inheritance
写在Doris毕业后的第一天
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
One of the first steps to redis
[零基础学IoT Pwn] 复现Netgear WNAP320 RCE
关于软件测试的一些思考
Error-tf.function-decorated function tried to create variables on non-first call
MongoDB第二話 -- MongoDB高可用集群實現
Opencv learning notes 5 -- document scanning +ocr character recognition
Solidty智能合约开发-简易入门
opencv学习笔记四--银行卡号识别
Build MySQL master-slave server under Ubuntu 14.04
Salesforce, Johns Hopkins, Columbia | progen2: exploring the boundaries of protein language models
Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
[14. Interval sum (discretization)]
Flink 系例 之 TableAPI & SQL 与 Kafka 消息插入
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
Ubuntu 14.04下搭建MySQL主从服务器