美文网首页
箭头函数,函数默认参数,数组和对象的解构

箭头函数,函数默认参数,数组和对象的解构

作者: 小菜鸟Soul | 来源:发表于2018-05-28 15:41 被阅读0次

箭头函数

ES6更新引入了箭头函数,这是声明和使用函数的另一种方式。它带来的好处:

  • 更简洁
  • 函数式编程
  • 隐式返回

示例代码:

  • 简洁
function double(x) { return x * 2; } // Traditional way
console.log(double(2)); // 4
  • 隐式返回
const double = x => x * 2; // Same function written as an arrow function with implicit return
console.log(double(2));  // 4
  • 作用域

在箭头函数中,this就是定义时所在的对象,而不是使用时所在的对象。基本上,使用箭头函数,您不必在调用函数内部的函数之前执行that = this技巧。

function myFunc() {
  this.myVar = 0;
  setTimeout(() => {
    this.myVar++;
    console.log(this.myVar) // 1
  }, 0);
}
详细介绍
简洁

在许多方面,箭头函数比传统函数更简洁。

  • 隐式VS显示返回

一个显示返回是其中一个函数返回关键字在它的身上使用。

function double(x) {
    return x * 2; // this function explicitly returns x * 2, *return* keyword is used
  }

在传统的函数中,返回总是显而易见的。但是使用箭头函数,您可以执行隐式返回,这意味着您不需要使用关键字return来返回值。

const double = (x) => {
    return x * 2; // 显示 return
  }

由于这个函数只返回一些东西(在return关键字之前没有指令),我们可以做一个隐式返回。

  const double = (x) => x * 2; // 隐式  return x*2

为此,只需要删除括号和返回关键字。这就是为什么它被称为隐式返回,return关键字不存在,但是这个函数确实会返回x * 2

注意:如果函数没有返回一个值(带有副作用),它不会执行显式或隐式返回。

另外,如果想隐式地返回一个对象,必须在它周围有括号,因为它会和块大括号冲突:

const getPerson = () => ({ name: "Nick", age: 24 })
console.log(getPerson()) // { name: "Nick", age: 24 } -- 函数隐式返回对象

  • 只有一个参数

如果函数只有一个参数,可以省略它周围的括号:

  const double = (x) => x * 2; // this arrow function only takes one parameter

参数周围括号可以省略:

  const double = x => x * 2; // this arrow function only takes one parameter

  • 没有参数

当没有参数提供给箭头函数时,需要提供圆括号,否则它将不是有效的语法。

() => { // parentheses are provided, everything is fine
    const x = 2;
    return x;
  }
=> { // No parentheses, this won't work!
    const x = 2;
    return x;
  }
  • this作用域

在箭头函数中,this就是定义时所在的对象,而不是使用时所在的对象。
没有箭头函数,如果想在函数内的某个函数中从这个函数访问一个变量,必须使用that=this或者self=this这个技巧。

示例代码

在myFunc中使用setTimeout函数:

function myFunc() {
  this.myVar = 0;
  var that = this; // that = this trick
  setTimeout(
    function() { // A new *this* is created in this function scope
      that.myVar++;
      console.log(that.myVar) // 1

      console.log(this.myVar) // undefined -- see function declaration above
    },
    0
  );
}

但是有了箭头功能,这是从其周围取得的

function myFunc() {
  this.myVar = 0;
  setTimeout(
    () => { // this taken from surrounding, meaning myFunc here
      this.myVar++;
      console.log(this.myVar) // 1
    },
    0
  );
}

函数默认参数值

从ES6开始,可以使用以下语法将默认值设置为函数参数:

function myFunc(x = 10) {
  return x;
}
console.log(myFunc()) // 10 -- no value is provided so x default value 10 is assigned to x in myFunc
console.log(myFunc(5)) // 5 -- a value is provided so x is equal to 5 in myFunc

console.log(myFunc(undefined)) // 10 -- undefined value is provided so default value is assigned to x
console.log(myFunc(null)) // null -- a value (null) is provided, see below for more details

函数默认参数应用于两种情况b并且只有两种情况:

  • 没有提供参数
  • 提供未定义的参数

如果传入null,默认参数将不会被应用

注意:默认值分配也可以与解构参数一起使用

对象和数组的解构

解构是通过从存储在对象或数组中的数据中提取一些值来创建新变量的一种便捷方式。

示例代码:
  • 对象
const person = {
  firstName: "Nick",
  lastName: "Anderson",
  age: 35,
  sex: "M"
}

没有解构

const first = person.firstName;
const age = person.age;
const city = person.city || "Paris";

随着解构,所有在一行中:

const { firstName: first, age, city = "Paris" } = person; // That's it !

console.log(age) // 35 -- A new variable age is created and is equal to person.age
console.log(first) // "Nick" -- A new variable first is created and is equal to person.firstName
console.log(firstName) // ReferenceError -- person.firstName exists BUT the new variable created is named first
console.log(city) // "Paris" -- A new variable city is created and since person.city is undefined, city is equal to the default value provided "Paris".

注意:在const关键字const { age } = person;之后的括号不用于声明对象或块,而是解构语法。

  • 函数参数

没有解构

function joinFirstLastName(person) {
  const firstName = person.firstName;
  const lastName = person.lastName;
  return firstName + '-' + lastName;
}

joinFirstLastName(person); // "Nick-Anderson"

在解构对象参数的过程中,得到一个更简洁的函数:

function joinFirstLastName({ firstName, lastName }) { // we create firstName and lastName variables by destructuring person parameter
  return firstName + '-' + lastName;
}

joinFirstLastName(person); // "Nick-Anderson"

解构使用箭头函数:

const joinFirstLastName = ({ firstName, lastName }) => firstName + '-' + lastName;

joinFirstLastName(person); // "Nick-Anderson"
  • 数组

定义一个数组:

const myArray = ["a", "b", "c"];

没有解构

const x = myArray[0];
const y = myArray[1];

解构

const [x, y, ...z] = myArray; // That's it !

console.log(x) // "a"
console.log(y) // "b"
console.log(z) // ["c"]
参考资料

相关文章

  • ES6常用特性与一些注意点

    数组解构 对象解构 字符串扩展方法 函数参数:默认值与剩余参数 箭头函数 对象字面量增强 Object.assig...

  • ES6学习笔记

    1. 变量的解构赋值 数组解构 对象解构 2. 箭头函数,rest参数 箭头函数 rest 参数 3. 数组: 扩...

  • ES6 ---- 函数

    函数参数的默认值 与解构赋值配合使用 rest参数 箭头函数 使用箭头函数的注意点 函数体内的this对象,是定义...

  • 箭头函数,函数默认参数,数组和对象的解构

    箭头函数 ES6更新引入了箭头函数,这是声明和使用函数的另一种方式。它带来的好处: 更简洁 函数式编程 隐式返回 ...

  • ES6和ES5对比

    函数默认值 箭头函数(函数表达式 --> 箭头函数) 处理多个返回值 (解构) 从 arguments 到剩余参数...

  • 函数扩展

    1、参数给定默认值 2、剩余参数 3、函数传参数的同时可以解构 3.1数组解构 3.2对象解构

  • ES6 概述(2)

    数组的解构和赋值 二维数组 对象定义:一组无序属性的集合 箭头函数 有名函数

  • ES6-函数参数的赋值

    函数参数的解构赋值: 函数参数解构赋值的默认值: 函数参数解构赋值的默认值undefined:

  • 2018-01-10 -03ES6阮一峰教程摘记3 函数的扩

    7.函数的扩展 1.参数默认值 && 函数参数结合解构赋值 2.rest参数: 形式:...变量名 3.箭头函数 ...

  • JS中常用的代码简写技巧

    Spread Operator 解构符 变量赋值 对象属性:属性名和值一样时的简写 箭头函数隐式返回 默认参数值 ...

网友评论

      本文标题:箭头函数,函数默认参数,数组和对象的解构

      本文链接:https://www.haomeiwen.com/subject/jmegjftx.html