当前位置:网站首页>Apprentissage avancé des fonctions en es6
Apprentissage avancé des fonctions en es6
2022-07-07 10:06:00 【Quand Xiaobai réussira - t - il?】
1、Définition de la fonction
//1.Fonction personnalisée(Fonctions nommées)
function fn(){
}
//2.Expression de la fonction(Fonction anonyme)
var fun = function(){
console.log("fun");
}
fun();
//3.Utilisationnew Function('Paramètres1','Paramètres2','Corps fonctionnel')
var f = new Function('a','b','console.log(a+b)');
f(1,2);La fonction appartient également à l'objet.
2、Appel de fonction
//1.Fonction normale
function fn(){
console.log("this is a fun");
}
fn();//Ou adoptionfn.call();
//2.Méthode de l'objet
var o ={
sayHi :function(){
console.log("this is a object fun");
}
}
o.sayHi();
//3.Constructeur
function Star(){
console.log('constructor fun');
}
let ldh = new Star();
//4.BIND Event Function
btn.onclick = function(){
console.log('btn click fun')
};//Cette fonction peut être appelée en cliquant sur le bouton
//5.Fonction Timer
setInterval(function(){console.log('setInterval fun')},1000);
//6.Exécuter la fonction maintenant
(function(){
console.log('lijizhixing ');
})()3、thisProblème de pointage
//1.Fonction normale-thisPointagewindow
function fn(){
console.log('1fn:'+this);
}
window.fn();
//2.Méthode de l'objet-thisPointageoCet objet
var o ={
sayHi :function(){
console.log(this);
}
}
o.sayHi();
//3.Constructeur-thisPointageldhCet objet d'instance
function Star(){
console.log(this);
}
let ldh = new Star();
//4.BIND Event Function-thisPointagebtnCe bouton
btn.onclick = function(){
console.log(this);
};
//5.Fonction Timer-thisPointagewindow
window.setTimeout(function(){console.log(this);},1000);
//6.Exécuter la fonction maintenant-thisPointagewindow
(function(){
console.log(this);
})()Résumé:
| Mode d'appel | thisPointage |
| Fonction normale | window |
| Constructeur | Objet instance( Les méthodes de l'objet prototype pointent également vers l'objet instance ) |
| Méthode objet | Objet auquel appartient la méthode |
| Liaison de l'événement | Lier l'objet de l'événement |
| Minuterie | window |
| Exécuter la fonction maintenant | window |
4、Modifier l'intérieur de la fonctionthisPointage
Les plus courants sont:call()、apply()、bind()Trois approches
1、callMéthodes
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
fn.call(o,1,2);//callComment utiliser
//Principales applications-Succession
function Father(uname,age)
{
this.uname = uname;
this.age = age;
}
function Son(uname,age)
{
Father.call(this,uname,age);
console.log('this name:'+this.uname);
}
let son1 = new Son('liming',18);
2、applyMéthodes
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
fn.apply(o,[2,3]);//applyUtilisation de, Les paramètres suivants doivent être un tableau
// Principaux cas d'application -UtilisationMathMax
var arr =[1,3,5,66,23,123];
let maxNum = Math.max.apply(Math,arr);
let minNum = Math.min.apply(Math,arr);
console.log("maxNum:"+maxNum+',minNum:'+minNum);3、bindMéthodes(Utilisation maximale)
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
//Méthode d'utilisation I
let newFn = fn.bind(o);//bindUtilisation de, Il n'appelle pas la fonction ,Renvoie la valeur spécifiée parthis Copie de la fonction originale de
newFn(2,3);
//Méthode d'utilisation II
let newFn2 = fn.bind(o,2,3);//bindUtilisation de, Il n'appelle pas la fonction ,Renvoie la valeur spécifiée parthis Copie de la fonction originale de
newFn2();Cas d'application principal I
var o ={
name:'liming',
fun:function(){console.log('object');}
}
function fn(a,b)
{
console.log(this);
console.log(a+b);
}
//Utiliser
let newFn = fn.bind(o);//bindUtilisation de, Il n'appelle pas la fonction ,Renvoie la valeur spécifiée parthis Copie de la fonction originale de
newFn(2,3);
//Principales applications
btn.onclick = function(){
this.disabled = true;//Ici.thisPointer vers le bouton
setTimeout(function(){
this.disabled = false;//Ici.thisPointagewindow
}.bind(this),3000);//bindAprès la liaison,Au - dessusthis Changer en bouton
//Équivalent à
// let fun =function(){
// this.disabled = false;//Ici.thisPointagewindow
// }.bind(this);
// setTimeout(fun,3000);//bindAprès la liaison,Au - dessusthis Changer en bouton
}Points de différenciation:
- callEtapplyLa fonction est appelée,Et changer l'intérieur de la fonctionthisPointage.
- callEtapplyLes paramètres passés sont différents,callParamètres de passagearg1,arg2...,applyDoit être sous forme de tableau[arg1,arg2...]
- bindLa fonction n'est pas appelée,Il est possible de modifier lethisPointage
Principaux scénarios d'application:
- callHériter souvent
- applySouvent associé à un tableau. Par exemple, en utilisant un objet mathématique pour atteindre la valeur maximale minimale d'un tableau
- bindNe pas appeler la fonction,Mais je veux aussi changerthisPointage. Par exemple, changer l'intérieur d'un minuteur thisPointage.
边栏推荐
- CDZSC_ 2022 winter vacation personal training match level 21 (2)
- CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)
- EXT2 file system
- Finally, there is no need to change a line of code! Shardingsphere native driver comes out
- ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
- The landing practice of ByteDance kitex in SEMA e-commerce scene
- ORM--查询类型,关联查询
- Parameter sniffing (1/2)
- 使用BigDecimal的坑
- Sword finger offer II 107 Distance in matrix
猜你喜欢

Use 3 in data modeling σ Eliminate outliers for data cleaning

使用BigDecimal的坑
![[Frida practice]](/img/20/fc68bcf2f55b140d6754af6364896b.png)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform

基础篇:带你从头到尾玩转注解

EXT2 file system

Detailed explanation of diffusion model

中国首款电音音频类“山野电音”数藏发售来了!

Performance optimization record of the company's product "yunzhujia"

小程序实现页面多级来回切换支持滑动和点击操作

A wave of open source notebooks is coming
随机推荐
CodeForces - 1324D Pair of Topics(二分或双指针)
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
2020CCPC威海 J - Steins;Game (sg函数、线性基)
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Can't connect to MySQL server on '(10060) solution summary
flinkcdc采集oracle在snapshot阶段一直失败,这个得怎么调整啊?
EXT2 file system
根据热门面试题分析Android事件分发机制(一)
How to use Mongo shake to realize bidirectional synchronization of mongodb in shake database?
根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
Write VBA in Excel, connect to Oracle and query the contents in the database
Arthas simple instructions
Main (argc, *argv[]) details
中国首款电音音频类“山野电音”数藏发售来了!
使用BigDecimal的坑
一大波开源小抄来袭
Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
虚数j的物理意义
网上可以开炒股账户吗安全吗
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do