ES6之函数优化(默认参数、剩余参数、 箭头函数)

一、默认参数

在ES6之前,我们无法给一个参数设置默认值,我们只能采取变通的写法:

 function add(a , b) {
    // 判断b是否为空,为空就给默认值1
    b = b || 1;
    return a + b;
 }
  // 传一个参数
  console.log(add(10));

现在es6可以这么写:

function add(a , b = 1) {
  return a + b;
}
// 传一个参数
console.log(add(10));

二、剩余参数

ES6中,当参数个数无法确定时,可以使用剩余参数(rest parameter)来表示,剩余参数就相当于一个容器,调用函数时传入几个参数值,这个容器就装载几个参数值。剩余参数能够将多个独立的参数合并到一个数组中去,剩余参数表示为...keys,有三个点加上一个具名参数标识符组成。

function fn(...keys){
	console.log(keys)
}
fn(11,22,'aa'); // [11, 22, "aa"]

三、箭头函数

箭头函数相当于匿名函数,并且简化了函数定义。

1. 简洁的语法

箭头函数使用=>符号来定义函数,相比普通函数更加简洁。
这种简洁性不仅体现在函数声明的方式上,还体现在可以省略function关键字、圆括号和大括号。

let fun = (obj) => {
    console.log(obj)
};

2. 隐式返回

如果箭头函数只有一条语句,那么可以省略大括号,并且自动将该语句的结果作为返回值。这种隐式返回的特性进一步简化了函数的书写。

  • 一个参数时()可以省略

    var print = obj => console.log(obj);
    
  • 多个参数()不可省略

    var sum2 = (a,b) => a+b;
    
  • 没有参数()不可省略

    var sayHello = () => console.log("hello!");
    

3. this指向

箭头函数没有自己的this值,它会捕获其所在上下文的this值作为自己的this(会在自己作用域的上层继承this)。箭头函数内部的this是词法作用域,由上下文确定

var age = 18;
var obj = {
    name: '张三',
    age: 20,
    getAge1() {
        var fn = function () {
            return this.age // 匿名函数的this指向window
        }
        return fn()
    },
    getAge2() {
        var fn = () => this.age; // 箭头函数的this总是指向词法作用域,也就是外层调用者obj
        return fn()
    }
}
alert(obj.getAge1()); // 18
alert(obj.getAge2()); // 20

call()、apply()、bind()等方法不能改变箭头函数中this的指向

var id = 'Global';
let fun1 = () => {
    console.log(this.id)
};
fun1();                     // 'Global'
fun1.call({id: 'Obj'});     // 'Global'
fun1.apply({id: 'Obj'});    // 'Global'
fun1.bind({id: 'Obj'})();   // 'Global'

4. 不能用作构造函数

由于箭头函数没有自己的this值,因此不能使用new关键字来调用箭头函数创建对象实例。
同时,箭头函数也没有prototype属性

const arrowFunc = () => {  }; 

// 不能用new来创建实例
new arrowFunc();  // Uncaught TypeError: arrowFunc is not a constructor

// 没有prototype属性
console.log(arrowFunc.prototype  === undefined);  // true

5. 没有arguments对象

箭头函数没有自己的arguments对象,因此不能通过arguments来获取函数参数列表。

const arrowFunc = (a,b,c) => {  
    console.log(arguments);
};  
  
arrowFunc(1, 2, 3); // Uncaught ReferenceError: arguments is not defined

不过,可以通过展开操作符(…)或使用默认参数来获取参数列表。

const arrowFunc = (...args) => {  
    console.log(args); // 输出参数列表的数组  
};  
  
arrowFunc(1, 2, 3); // 输出 [1, 2, 3]
const arrowFunc = (param1 = 'default1', param2 = 'default2') => {  
    console.log(param1, param2);  
};  
  
arrowFunc(); // 输出 'default1' 'default2'  
arrowFunc('value1'); // 输出 'value1' 'default2'  
arrowFunc('value1', 'value2'); // 输出 'value1' 'value2'

6. 不能用作生成器函数

箭头函数不能使用yield关键字来定义生成器函数,因此无法迭代返回多个值。


四、对象的函数属性的简写

let person = {
    name: "jack",
    // es5写法:
    eat: function (food) {
        console.log(this.name + "在吃" + food);
    },
    // es6箭头函数版:
    eat2: food => console.log(person.name + "在吃" + food), // 这里拿不到this
    // es6简写版:
    eat3(food) {
        console.log(this.name + "在吃" + food);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫老板的豆

你的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值