ES6+常用语法1

96 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

声明

  1. let

    let a = 1
    let b = 2, c = 3
    
    console.log(a, b, c) // 1 2 3
    

    let 用于声明一个块级作用域的本地变量,不允许重复声明且不会造成变量提升。

    let 的出现解决了 var 造成变量提升、覆盖和泄露等问题。

  2. const

    const a = 1
    
    console.log(a) // 1
    

    const 用于声明常量,即不允许重新赋值(修改变量指向的内存地址),也不允许重复声明。不会造成变量提升。

解构

  1. 解构对象

    const tom = {
      name: 'Tom',
      age: 18,
      city: 'Beijing',
    }
    
    // 基础用法
    const { name, age } = tom
    console.log(name, age) // Tom 18
    
    // 属性重命名
    const { name: myName } = tom
    console.log(myName) // Tom
    
    // 设置默认值
    const { city, country = 'China' } = tom
    console.log(city, country) // Beijing China
    
  2. 解构数组

    const arr = [1, 2, 3]
    
    const [a, b, c] = arr
    
    console.log(a, b, c) // 1 2 3
    

    如果数组的长度不一致,可以使用 ... 来解构,只保留长度一致的部分。

    const arr = [1, 2, 3]
    
    const [a, ...rest] = arr
    
    console.log(a, rest) // 1 [2, 3]
    
    const [a, b, c, ...rest] = arr
    
    console.log(a, b, c, rest) // 1 2 3 []
    
  3. 解构函数参数

    function foo({ name, age }) {
      console.log(name, age)
    }
    
    foo({ name: 'Tom', age: 18 }) // Tom 18
    
    function bar([a, b, c]) {
      console.log(a, b, c)
    }
    
    bar([1, 2, 3]) // 1 2 3
    
  4. 解构字符串

    const str = 'hello'
    
    const [a, b, c, d] = str
    
    console.log(a, b, c, d) // h e l l
    

字符串

  1. 模板字符串

    const name = 'Tom'
    const age = 18
    
    const str = `Hello, I'm ${name}, ${age} years old.`
    console.log(str) // Hello, I'm Tom, 18 years old.
    

    模板字符串的出现,是字符串的一个重要特性,它可以提供更加灵活的字符串拼接方式。

  2. startsWith()

    const str = 'hello'
    
    console.log(str.startsWith('h')) // true
    console.log(str.startsWith('e')) // false
    

    startsWith() 方法用于判断字符串是否以指定的字符串开头,如果是,则返回 true,否则返回 false。

  3. endsWith()

    const str = 'hello'
    
    console.log(str.endsWith('o')) // true
    console.log(str.endsWith('e')) // false
    

    endsWith() 方法用于判断字符串是否以指定的字符串结尾,如果是,则返回 true,否则返回 false。

  4. includes()

    const str = 'hello'
    
    console.log(str.includes('h')) // true
    console.log(str.includes('e')) // true
    console.log(str.includes('x')) // false
    

    includes() 方法用于判断字符串是否包含指定的字符串,如果是,则返回 true,否则返回 false。

  5. repeat()

    const str = 'ha~'
    
    console.log(str.repeat(3)) // ha~ha~ha~
    

    repeat(n) 方法用于将一个字符串重复 n 次,如果 n 是小于 1 的数值,则返回一个空字符串。

数字

  1. Math.sign()

    const a = -3
    const b = 0
    const c = 3
    
    console.log(Math.sign(a)) // -1
    console.log(Math.sign(b)) // 0
    console.log(Math.sign(c)) // 1
    

    Math.sign() 方法用于获取一个数的符号,如果是正数,则返回 1,如果是负数,则返回 -1,如果是 0,则返回 0。

数组

  1. Array.from()

    const arrayLike = {
      0: 'a',
      1: 'b',
      2: 'c',
      length: 3,
    }
    console.log(Array.from(arrayLike)) // ['a', 'b', 'c']
    
    const str = 'hello'
    console.log(Array.from(str)) // ['h', 'e', 'l', 'l', 'o']
    
    const mySet = new Set([1, 2, 3])
    console.log(Array.from(mySet)) // [1, 2, 3]
    
    const myMap = new Map([  ['a', 1],
      ['b', 2],
      ['c', 3],
    ])
    console.log(Array.from(myMap)) // [['a', 1], ['b', 2], ['c', 3]]
    

    Array.from() 方法用于将一个类数组(伪数组)或者可迭代对象转换成一个真正的数组。

  2. Array.of()

    console.log(Array.of(1, 2, 3)) // [1, 2, 3]
    

    Array.of() 方法用于将一组值,转换成一个数组。

  3. Array.prototype.find()

    const studentList = [
      {
        id: 1,
        name: 'Tom',
        age: 12,
      },
      {
        id: 2,
        name: 'Jerry',
        age: 13,
      },
      {
        id: 3,
        name: 'Bob',
        age: 11,
      },
    ]
    
    const student = studentList.find(item => item.id === 2)
    
    console.log(student) // { id: 2, name: 'Jerry', age: 13 }
    

    find() 方法用于返回数组中满足提供的测试函数的第一个元素的值。

  4. Array.prototype.findIndex()

    const studentList = [
      {
        id: 1,
        name: 'Tom',
        age: 12,
      },
      {
        id: 2,
        name: 'Jerry',
        age: 13,
      },
      {
        id: 3,
        name: 'Bob',
        age: 11,
      },
    ]
    
    const index = studentList.findIndex(item => item.id === 2)
    
    console.log(index) // 1
    

    findIndex() 方法用于返回数组中满足提供的测试函数的第一个元素的索引。

  5. Array.prototype.fill()

    const arr = [1, 2, 3, 4, 5]
    
    console.log(arr.fill(0)) // [0, 0, 0, 0, 0]
    console.log(arr.fill(0, 1)) // [1, 0, 0, 0, 0]
    console.log(arr.fill(0, 1, 3)) // [1, 2, 0, 0, 0]
    

    fill() 方法用于使用一个固定值,填充一个数组。

  6. Array.prototype.copyWithin()

    const arr = [1, 2, 3, 4, 5]
    
    console.log(arr.copyWithin(0, 3)) // [4, 5, 3, 4, 5]
    console.log(arr.copyWithin(0, 3, 4)) // [4, 2, 3, 4, 5]
    

    copyWithin() 方法用于将一个数组的元素复制到另一个位置,然后返回这个数组。

  7. Array.prototype.entries()

    const arr = [1, 2, 3, 4, 5]
    
    for (const [index, value] of arr.entries()) {
      console.log(index, value)
    }
    
    // 0 1
    // 1 2
    // 2 3
    // 3 4
    // 4 5
    

    entries() 方法返回一个新的 Array Iterator 对象,该对象包含数组中每个索引的键值对。