当前位置:网站首页>es6新增-函数部分
es6新增-函数部分
2022-07-17 05:05:00 【卷心菜007】
函数的扩展
1.es6参数扩展:
(1)默认值
在形参默认的位置,通过单等号赋值即可,仅没传参数及参数为undefined时走默认值
function add(x=10,y=20){ return x+y }
console.log(add(10,10)) //20
console.log(add(' ',0)) // " 0 "
console.log(add(undefined,10)) //20
(2)可变参数
...args收集剩余参数 args:参数名
注意: ...args为真数组,es5中arguments为伪数组
注意:... 只能出现一次; ... 只能放在最后一个形参位置; ... 前面可以有一个或多个固定位置参数
function getSum(...args){ console.log(args) }
console.log(1,2,3,4,5) //输出[1,2,3,4,5]
function getArr( ...args,...rest){} //两个 ... 同时出现,会报错
function getSum(num,..args){ console.log(num,args)}
console.log(gerSum(100,1,2,3,4,5)) //输出:100,[1,2,3,4,5]
2.es6中新增箭头函数
1,语法:const 函数名 = () => { }
2.特性:
(1)形参只有一个时,可以省略小括号
(2)函数体只有一句话的时候,可以省略大括号,如果省略了大括号,箭头函数自带return功能,不用再写
(3)箭头函数内部的this指向父级函数中的this
3.注意:不能通过new关键字调用
4,应用:多用在回调函数及改变this指向的时候
普通函数 const fu = function(){ }
箭头函数 const fn=()=>{ 函数体 } fn()
const sayHi = function(name){ return `hi${this.name}`}
const sayHi = (name)=>{ `hi${this.name}` } sayHi("aa")
边栏推荐
- About the current response, the method getoutputstream() has been called
- Internship project 2 - Homepage configuration - my data module
- 热更新及其原理
- Installation and fast use of Mongo DB stand-alone version
- IDL 6S查找表
- C语言练习2
- Notes de formation pour la deuxième fois des modèles
- 实习项目3-更改所有者
- STL容器——vector的基本操作
- uniapp中使用ucharts图表,饼状图,柱状图,折线图
猜你喜欢

基于cuda10.0的pytorch深度学习环境配置

ModerlArts第一次培训笔记

数据分析与数据挖掘实战案例本地房价预测(716):

IText modify PDF Text

Mysql database experiment training 6, data view (detailed)

ThreadLocal thread safety example and its principle

uniapp中使用ucharts图表,饼状图,柱状图,折线图

Word2Vec原理及应用与文章相似度(推荐系统方法)

SQL statement learning

About the current response, the method getoutputstream() has been called
随机推荐
Harmonyos second training notes
6S参数
NVIDIA GeForce Experience登录报错:验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序(解决办法)
Baidu map realizes thermal map
【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
学习C语言第二天
IDL 读取葵花8(Himawari-8)HSD数据
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
02_電影推薦(ContentBased)_用戶畫像
Fanoutexchange switch is simple to use
Ucharts chart, pie chart, bar chart and line chart are used in uniapp
STL容器——map的基本操作
数据库实训7【索引与数据完整性约束的创建】
微信小程序云开发使用方法-1
轮播图的两种方法及自动轮播
Use of transactions - Django, SQL tools
Internship project 2 - Homepage configuration - my data module
Uniapp uses uview to realize folding panel
es6新增-数组部分
es6新增-运算符的扩展