当前位置:网站首页>ES6新特性分享(三)
ES6新特性分享(三)
2022-07-22 10:06:00 【五花肉三七分】
ES6新特性分享(三)
一.箭头函数
ES6 允许使用「箭头」(=>)定义函数。
//声明一个函数
// let fn = function(){
// }
//箭头函数
// let fn = (a,b) => {
// return a + b;
// }
箭头函数的注意点:
1) 如果形参只有一个,则小括号可以省略
/**
* 省略小括号的情况
*/
let fn2 = num => {
return num * 10;
};
2) 函数体如果只有一条语句,则花括号可以省略,函数的返回值为该条语句的执行结果
/**
* .省略花括号的情况
*/
let fn3 = score => score * 20;
3) 箭头函数 this 指向声明时所在作用域下 this 的值
/**
* this 指向声明时所在作用域中 this 的值
*/
let fn4 = () => {
console.log(this);
let shuaige = {
name: '奇奇',
getName(){
let fn5 = () => {
console.log(this);
}
fn5();
}
};
4) 箭头函数不能作为构造函数实例化
//运行看效果
// let Person = (name, age) => {
// this.name = name;
// this.age = age;
// }
// let me = new Person('xiao',30);
// console.log(me);
5) 不能使用 arguments
//运行看效果
// let fn = () => {
// console.log(arguments);
// }
// fn(1,2,3);
箭头函数常用来指定回调函数,因为他不会更改this的指向
二. rest 参数
ES6 引入 rest 参数,用于获取函数的实参,用来代替 arguments
/**
* 作用与 arguments 类似
*/
function add(...args){
console.log(args);
}
add(1,2,3,4,5);
/**
* rest 参数必须是最后一个形参
*/
function minus(a,b,...args){
console.log(a,b,args);
}
minus(100,1,2,3,4,5,19);
rest 参数非常适合不定个数参数函数的场景
结尾
继续加油。
边栏推荐
- ansible简单使用示例
- 索引为什么选择B+树
- Ci24r1 low-cost 2.4G wireless transceiver chip replaces xn297 compact si24r1
- Why does additional currency issue cause inflation? How to cause
- -Bash: ifconfig: command not found
- SI14T触摸按键芯片兼容替代TMS12
- Sentry nodestore_node 表中的数据查看
- 08.01 邻接矩阵
- SQLite UDF/UDAF 的 PHP 实现
- Pytest interface automated testing framework | plug-in secondary development practice
猜你喜欢
随机推荐
相比技术与产品 倾听与意见才是Web3当下的首要任务
分享一下Typora工具
派生类成员的访问
How maxscale handles the state of event after MariaDB master-slave switch -handle_ events
派生类的构造函数和析构函数
Sentry nodestore_node 表中的数据查看
MySQL 查看表结构最后变更时间
No code coverage driver is available
firewall-cmd 常用操作命令
中国最幸运极客:二十出头身家过亿,三次创业全身而退
mysql进阶(十七)Cannot Connect to Database Server问题分析
mysql 将毫秒数转为时间字符串
派生類的構造函數和析構函數
Definition of derived classes
多态性
7.21 permutation and binary
Connectivity of digraph
DP4361国产六通道立体声D/A音频转换器芯片替代CS4361
PHP implementation of SQLite udf/udaf
mariadb审计插件