当前位置:网站首页>Utilisation des fonctions fléchées es6
Utilisation des fonctions fléchées es6
2022-07-23 22:31:00 【InfoQ】
Préface
On y va.
function fun() {
return 100;
}
console.log(fun()); //100const fun = function() {
return 100;
}
console.log(fun()); //100const fun1 = () => {
return 100;
}
console.log(fun1()); //100const fun2 = x => x;
console.log(fun2(100)); //100- ()Définir les paramètres dans,S'il n'y a qu'un seul paramètre,()Peut être omis
- {}Corps de la fonction d'écriture moyenne,Si vous n'avez que des valeurs de retour dans le corps de la fonction,Peut ne pas écrirereturn
La différence entre une fonction fléchée et une fonction normale
Par exemple,
let obj = {
name: 'Xiao Ming',
age: 3,
sayName() {
setTimeout(function() {
console.log("Je suis" + this.name);
}, 500)
}
}
obj.sayName();Je suisundefinedlet obj = {
name: 'Xiao Ming',
age: 3,
sayName() {
setTimeout(function() {
console.log(this);
}, 500)
}
}
obj.sayName();let obj = {
name: 'Xiao Ming',
age: 3,
sayName() {
let self = this;
setTimeout(function() {
console.log("Je suis" + self.name);
}, 500)
}
}
obj.sayName();C'est Xiao Ming.Utiliser les fonctions fléchées
let obj = {
name: 'Xiao Ming',
age: 3,
sayName() {
setTimeout(() => {
console.log("Je suis" + this.name);
}, 500)
}
}
obj.sayName();C'est Xiao Ming. Je pense que vous et moi avons le même doute : Pourquoi est - ce possible avec les fonctions fléchées ?La différence entre une fonction fléchée et une fonction normale
- thisDirection différente
- Fonction normale:Qui appelle cette fonction,
thisÀ qui? - Fonctions fléchées: Où définir la fonction ,
thisÀ qui?
Queue de tranche
边栏推荐
猜你喜欢

Still worrying about xshell cracking, try tabby

巴菲特股东大会的启示 2021-05-06

LeetCode高频题53. 最大子数组和,具有最大和的连续子数组,返回其最大和

ONEFLOW V0.8.0 officially released

Can Verilog of synthetizable be integrated

Microsoft SQL Server数据库语言及功能使用(十三)

JS——事件代理和应用场景

Linked list - 203. remove linked list elements

狂神redis笔记10

多线程问题:为什么不应该使用多线程读写同一个socket连接?
随机推荐
I, AI doctoral student, online crowdfunding research topic
Programming in the novel [serial 18] the moon bends in the yuan universe
Explain NAT technology in detail
Array - 977. Square of ordered array
LeetCode高频题53. 最大子数组和,具有最大和的连续子数组,返回其最大和
torchvision.datasets.ImageFolder前的数据整理及使用方法
[C language] address book (static version)
Memory search - DP
What else do entrepreneurs need besides money? Exclusive interview with Mingyue Lake venture capital institutions
El upload realizes the preview of uploaded files
[Matplotlib drawing]
小说里的编程 【连载之十七】元宇宙里月亮弯弯
121. The best time to buy and sell stocks
Wangxuegang video coding -- mediacodec coding and decoding
MySQL的 DDL和DML和DQL的基本语法
【Unity3D日常BUG】Unity3D解决“找不到类型或命名空间名称“XXX”(您是否缺少using指令或程序集引用?)”等问题
Excel password related
海外资深玩家的投资建议(2) 2021-05-03
接口测试
Leetcode high frequency question 62. different paths: how many paths does the robot have from the upper left corner to the lower right corner? Pure probability permutation and combination problem, not