1.参数默认值

在书写形参时,直接给形参赋值,赋的值即为默认值,这样一来,当调用函数时,如果没有给对应的参数赋值,则会自动使用默认值。

  1. // 原始
  2. function sum(a, b, c) {
  3. b = b === undefined && 1;
  4. c = c === undefined && 2;
  5. return a + b + c;
  6. }
  7. // console.log(sum(10));
  8. // ES6
  9. function sum1(a, b=1, c=2) {
  10. return a + b + c
  11. }
  12. console.log(sum1(10,undefined,undefined));

2.参数默认值对arguments的影响

只要给函数加上参数默认值,该函数会自动变成严格模式下的规则,arguments和形参脱离

3.暂时性死区

ES6中,形参和let或const声明一样,具有作用域,并且根据参数的声明顺序,存在暂时性死区。

4.传递不定长参数

  1. function sum(args){
  2. let sum = 0;
  3. for (let i = 0;i<args.length;i++){
  4. sum += args[i];
  5. }
  6. return sum;
  7. }
  8. console.log(sum([5,6,9]));
  1. function sum(){
  2. let sum = 0;
  3. for (let i = 0;i<arguments.length;i++){
  4. sum += arguments[i];
  5. }
  6. return sum;
  7. }
  8. console.log(sum(5,6,9));

但是通过arguments获取参数,会有以下缺陷:
1.如果和形参配合使用,容易导致混乱
2.从语义上,使用arguments获取参数,由于形参缺失,无法从函数定义上理解函数的真实意图
ES6中的剩余参数可以用于收集末尾的所有参数,将其放置到一个形参数组中。

  1. function sum(...args){
  2. let sum = 0;
  3. for (let i = 0;i<args.length;i++){
  4. sum += args[i];
  5. }
  6. return sum;
  7. }
  8. console.log(sum(5,6,9,9));

注意: 一个函数只能出现一个剩余参数; 若一个函数有剩余参数,那么这个剩余参数必须是最后一个参数

5.展开运算符

  1. const arr1 = [3, 67, 8, 5];
  2. //克隆arr1数组到arr2
  3. const arr2 = [0, ...arr1, 1];
  4. console.log(arr2, arr1 === arr2)
  1. const obj1 = {
  2. name: "萌新",
  3. age: 18,
  4. loves: ["编程", "游戏","电影"],
  5. address: {
  6. country: "中国",
  7. province: "江苏",
  8. city: "昆山"
  9. }
  10. }
  11. // 浅克隆到obj2
  12. const obj2 = {
  13. ...obj1,
  14. name: "小白",
  15. address: {
  16. ...obj1.address
  17. },
  18. loves: [...obj1.loves, "电影"]
  19. };
  20. console.log(obj2)

6.明确函数

  1. function Person(firstName, lastName) {
  2. if(!(this instanceof Person)){
  3. throw new Error("该函数不是通过new的方式调用函数")
  4. }
  5. this.firstName = firstName;
  6. this.lastName = lastName;
  7. this.fullName = `${firstName} ${lastName}`;
  8. }
  9. const p2 = Person("萌", "新");
  10. console.log(p2);

ES6提供了一个特殊的API,可以使用该API在函数内部,判断该函数是否使用了new来调用 new.target 该表达式得到的是:如果没有使用new来调用函数,则返回undefined 如果使用new调用函数,则得到的是new关键字后面的函数本身

  1. function Person(firstName, lastName) {
  2. if (new.target === undefined) {
  3. throw new Error("该函数没有使用new来调用")
  4. }
  5. this.firstName = firstName;
  6. this.lastName = lastName;
  7. this.fullName = `${firstName} ${lastName}`;
  8. }
  9. const p3 = Person.call(new Person(), "萌", "新")
  10. console.log(p3);S

7.箭头函数

this指向: 通过对象调用函数,this指向对象 直接调用函数,this指向全局对象 通过new调用函数,this指向新创建的对象 通过apply,call,bind调用函数,this指向指定的数据 DOM事件函数,this指向事件源

  1. const obj = {
  2. count:0,
  3. start:function (){
  4. setInterval(function (){
  5. console.log(this);
  6. this.count++
  7. console.log(this.count++);
  8. },1000)
  9. },
  10. regEvent:function (){
  11. window.onclick = function (){
  12. console.log(this.count);
  13. }
  14. }
  15. }
  16. console.log(obj.start());
  17. console.log(obj.regEvent());

箭头函数是一个函数表达式,理论上任何使用函数表达式的场景,都可以使用箭头函数

  1. // 完整语法
  2. (参数1, 参数2, ...)=>{
  3. //函数体
  4. }
  5. // 如果参数只有一个,可以省略小括号
  6. 参数 => {
  7. }
  8. // 如果箭头函数只有一条返回语句,可以省略大括号,和return关键字
  9. 参数 => 返回值
  1. const obj = {
  2. count: 0,
  3. start: function () {
  4. setInterval(() => {
  5. this.count++;
  6. console.log(this.count);
  7. }, 1000)
  8. },
  9. regEvent: function () {
  10. window.onclick = () => {
  11. console.log(this.count);
  12. }
  13. },
  14. }
  15. console.log(obj.start());
  16. console.log(obj.regEvent());

说明: 箭头函数中,不存在this、arguments、new.target,如果使用了,则使用的是函数外层的对应的this、arguments、new.target 箭头函数没有原型 箭头函数不能作用构造函数使用

应用场景: 1.临时性使用的函数,并不会刻意调用它,比如: 1.事件处理函数 2.异步处理函数 3.其他临时性的函数 2.为了绑定外层this的函数 3.在不影响其他代码的情况下,保持代码的简洁,最常见的,数组方法中的回调函数