当前位置:网站首页>ES6中箭头函数 (=>)、三点运算符(...)的基本用法和注意事项(this指向)
ES6中箭头函数 (=>)、三点运算符(...)的基本用法和注意事项(this指向)
2022-07-13 17:05:00 【小余努力搬砖】
目录
一、箭头函数
基础语法:
() =>{} // ():函数 =>:必须的语法,指向代码块 {}:代码块
const realName = () => { } ;//把函数赋值给realName。
例:
const num = (k,v) => {
return k+v;
}省略写法:
如果形参或者代码块只有一句可以简写:
const myFun = (k) => {return k+1;}简写:
const myFun = k => k +1;this指向:
1.全局函数下的this
普通函数与调用者有关
箭头函数的this是静态 根据上下文的this
2.对象方法里面的this
这里的this是指向实例的
const Person = {
realname:"张三",age:19,
say:function(){
console.log(this);
}
}
Person.say(); //person实例3.对像里面的箭头函数this
这里的this指向window
const Person = {
realname:"张三",age:19,
say:()=>{
console.log(this);
}
}
Person.say();//windows4.构造函数中的this指向问题
普通的this可以改变实例
箭头函数中的this不会改变,一开始 new 的实例永远就是这个实例
function Person(realname,age){
this.realname = realname;
this.age = age;
this.say = ()=>{
console.log(this);//这个this不会 当时new 实例是谁就是谁
}
this.say1 = function(){
console.log(this);
}
}
const P1 = new Person("张三",19);
const P2 = new Person("李四",20);
P1.say.call(P2);//{realname: '张三', age: 19, say: ƒ, say1: ƒ}
P1.say1.call(P1);//{realname: '张三', age: 19, say: ƒ, say1: ƒ}
简单应用:
简单的点击事件,体现this的指向
<button id="btn">未点击</button>
<script>
let btnObj = document.querySelector("#btn");
let flag = false;
btnObj.addEventListener("click", function () {
flag = !flag;
if (flag) {
setTimeout(() => {
this.innerText = "已点击";//箭头this指向上下文
}, 3000)
} else {
this.innerText = "未点击"; //调用者
}
})
</script>二、三点运算符
1.函数传不定参数,验证数组的长度。
function demo(a,...b){
console.log(a,b);//b为数组 2,3,4,5
}
demo(1,2,3,4,5);2.与解构使用
let [a,...b] = [1,2,3,4,5,6]
console.log(a,b);//1,[2,3,4,5,6]3.与数组解构使用 函数传对象
function demo({username,password}){
console.log(username,password);
}
demo({username:'root',password:'123456'})4.扩展
1.用...输出数组
const arr = [1,2,3];
console.log(...arr);2.合并数组
const arr1 = [1,2,3];
const arr2 = [3,4,5];
const arr3 = [...arr1,...arr2];
console.log(arr3);3.将类数组转为真正的数组
<div>1</div>
<div>2</div>
<div>3</div>
<script>
let div = document.querySelectorAll('div');
const arr = [...div];
console.log(arr);
let str = '12345';
console.log([...str]);
</script>4.复制数组
let arr1 = [1,2,3];
let arr2 = [...arr1];
console.log(arr1,arr2);5.练习
let demo=(a,b)=> a+b;
const arr1 = [1,2];
const arr2 = [4,5];
console.log(demo(...arr1));//3
console.log(demo(...arr2));//9
边栏推荐
- error [XXX.zip]: start of central directory not found; zipfile corrup
- 手势方面论文列表
- How to write effective interface tests?
- Learning to Estimate 3D Hand Pose from Single RGB Image&amp笔记
- 又一款抓包工具,比Fiddler更好用的神器:Charles
- plantUML使用总结
- 一个整数队列的偶数放在前面,奇数放后面且偶数奇数的相对位置不变
- Expert, Alibaba Daniel quit and brought out the internal "high concurrency system design" learning manual
- 如何关闭卡死的程序进程?
- How can testing bring more value? Talk about test shift left and test shift right
猜你喜欢

目标检测

tensorflow 使用 深度学习(二)

fiddler和charles拦截并修改请求和返回值

如何实现数字化转型?麦肯锡:数字化转型四步法

Still alive, LETV has 400 employees: Immortals' means of making money have been exposed

ABAP BAPI 复制标准项目模板实现项目立项
![[mqtt from getting started to improving | 05] mqtt3.1.1 publish publishing workflow](/img/86/4c702f32c53fb8a84ac1ff7139cd4a.png)
[mqtt from getting started to improving | 05] mqtt3.1.1 publish publishing workflow

看看谷歌如何在目标检测任务使用预训练权值 | CVPR 2022

信息系统项目管理师必背核心考点(四十一)风险管理计划

【回归预测-LSTM】基于attention机制的LSTM实现时间序列回归预测附matlab代码
随机推荐
自定义类型详解(结构体、枚举,联合)
美团二面:为什么 Redis 会有哨兵?
draw.io图片保存路径设置
RK3568开发板安装系统开机
fxksmdb.exe进程说明
【路由优化】基于生物地理学优化的 HWSN 节能聚类协议附matlab代码
内行,阿里大牛离职带出内部“高并发系统设计”学习手册
mysql8.0.16安装之后更改datadir路径
【数字识别】基于知识库实现手写体数字识别附matlab代码
[route optimization] HWSN energy-saving clustering protocol based on biogeography optimization with matlab code
目标检测
螺旋矩阵
FLASH W74M12JWSSIQ_ W25q64fwzpig specification, memory
[mqtt from getting started to improving | 05] mqtt3.1.1 publish publishing workflow
看看谷歌如何在目标检测任务使用预训练权值 | CVPR 2022
不小心被拉进QQ诈骗群之后
数字藏品大热,年轻人快不够用了
The idea of making parent column template with multiple sub columns in the Torres intensive intelligence portal platform
Blockbuster: released by domestic IDE, developed by Alibaba, completely open source (high performance + high customization)
[底层原理]Socket 究竟是什么? 为啥网络离不开 Socket?