当前位置:网站首页>ES6 from getting started to mastering 06: arrow function this direction and precautions
ES6 from getting started to mastering 06: arrow function this direction and precautions
2022-07-22 05:31:00 【Kai Xiaomo】
explain
ES6 From entry to Mastery Series ( whole 23 speak ) Learning notes .
Arrow function this Point to
es5 Medium this Point to : Depends on the context object that calls the function
The arrow function doesn't this Point to . Inside the arrow function this The value can only be determined by looking up the scope chain .
Example :
let DocHandle = {
id: "kaimo313",
init: function() {
document.addEventListener("click", function(event) {
this.doSomeThings(event.type);
}, false);
},
doSomeThings: function(type) {
console.log(` Event type :${
type}, Current id:${
this.id}`);
}
}
DocHandle.init();
Click the document to report an error :this.doSomeThings is not a function
explain this There's something wrong with it , We can print , We found this this Yes document 了 :
es5 Processing mode : Use bind change this Point to .
let DocHandle = {
id: "kaimo313",
init: function() {
document.addEventListener("click", function(event) {
console.log(this);
this.doSomeThings(event.type);
}.bind(this), false);
},
doSomeThings: function(type) {
console.log(` Event type :${
type}, Current id:${
this.id}`);
}
}
DocHandle.init();
es6 Treatment mode :
We can't pack here init I'll change it to an arrow function , Otherwise this Will point to window: Because inside the arrow function this The value can only be determined by looking up the scope chain .
matters needing attention
1、 After using the arrow function , There is no inside function arguments.
let getVal = (a, b) => {
console.log(arguments);
return a + b;
}
getVal()
Because of the this Yes window.
2、 Arrow functions cannot be used new Keyword to instantiate an object .
function A function is also an object , But the arrow function is not an object , It is actually a grammatical sugar .
let Kaimo313 = function() {
};
let k313 = new Kaimo313();
console.log(k313);
let Kaimo = () => {
};
let k = new Kaimo();
console.log(k);
expand :
Built in function objects that are not identified as constructors do not implement [[Construct]]
Internal methods ,[[Construct]]
We use new or super Something to use when creating new objects , Only functions of type Normal
To construct and implement [[Construct]]
.
therefore , Only Normal
Function of type ( Also is to use function Keyword constructed function ) It can be used as a constructor , Other types of functions ( Arrow function 、 Method shorthand ,generator) Can't use constructors , in other words , Out-of-service new Operator call .
The definition created by each function boils down to FunctionCreate EcmaScript
Definition in the specification .
For details, please refer to 【 Beat the drum 】TypeError: “x” is not a constructor
边栏推荐
- 2022.7.9 summer personal training 1-c Clean up the Powers that Be
- 如何使用多类型数据预训练多模态模型?
- 蔚来「城市」探索之路
- Babbitt metauniverse daily must read: ask senior government officials to disclose all their investments in NFT? What else did the legal consultation released by the U.S. government ethics office say
- GTSAM入门学习
- 手机寒冬到来,三星被迫狙杀中国手机,苹果独站山头看风景
- 国产芯片价格超低在国产替代的同时大量出口,让美国芯片深感压力
- Static setting and baking properties of unity
- Pl/sql record
- 长期的远程工作面临的几个问题和持续改进的组织自动化
猜你喜欢
【攻防世界WEB】难度一星3分入门题:get、post、robots、、cookie、button、weak、php、web、unserialize
MySQL data type
获取美团,饿了么外卖cps和权益链接
2022.7.9 summer personal training 1-c Clean up the Powers that Be
[linked list and its classic problems]
美国急于发展6G或是重温坐收3G专利费的旧梦,但中国已抢占先机
Share an introductory guide to canvas
FTP service and configuration
Microservice technology development
判断一个人靠不靠谱,就看这3点
随机推荐
Operation of Py file
Codeforces Round #807 (Div. 2)
How to drive growth strategy with innovation
2022.7.9 summer vacation personal training 1-b.how old are you Mr. string
Some practical debugging skills of Chrome browser in Windows system
LeetCode·
【js基础】逻辑判断篇(易错点)
你离「TDengine 开发者大会」只差一条 SQL 语句!
py文件的运行
并联型模糊PID复合控制器的simulink建模与仿真
strcmp() - 比较字符串
ES6 从入门到精通 # 03:模板字符串
二叉树刷题(完结篇)
Chapter 12 principle and application of network security audit technology
Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接
“全光”时代的宠儿——400G光模块
单片机不支持printf 浮点类型的解决办法
国产芯片价格超低在国产替代的同时大量出口,让美国芯片深感压力
BraTS18——多模态MR图像脑肿瘤分割挑战赛续5
Automatic synchronization of server time