美文网首页每日一文
ES6常用特性与一些注意点

ES6常用特性与一些注意点

作者: 永恒即是最美 | 来源:发表于2021-02-18 15:19 被阅读0次

    数组解构

    const arr = [100, 200, 300]
    let [foo, bar, baz] = arr    //100 200 300
    let [, , baz] = arr    //300
    let [foo, ...rest] = arr    //100,[200,300]
    let [foo] = arr    //100
    let [foo, bar, baz = 400, more = 123] = arr    //100 200
    //解构时等号后为默认值
    let [foo, bar, baz = 400, more = 123,and] = arr    //100 200 300 123 undefined  
    

    对象解构

    const obj = { name: 'zs', age: 18 }
    const { name,age:newAge = '20' } = obj    //name值为'zs',newAge为18,覆盖默认值20 
    

    字符串扩展方法

    const msg = 'Error: foo is not defined.'
    //是否以某个字符串开始
    console.log(msg.startsWith('Error'))        //true
    //是否以某个字符串结尾
    console.log(msg.endsWith('.'))        //true
    //是否包含某个字符串
    console.log(msg.includes('foo'))        //true
    

    函数参数:默认值与剩余参数

    function foo(a=100,...args){
      console.log(a,args)
    }
    foo()      //100 []
    foo(101,10)    //101 [10]
    foo(200,300,400)    //200 [300,400]
    

    箭头函数

    const person = {
        name:'tom',
        say1:()=>{ console.log(this.name) },
        say2:function(){ console.log(this.name) }
    }
    person.say1()    //'' this为window
    person.say2()    //'tom' this为person
    

    对象字面量增强

    const bar = "bar"
    const obj = {
      name: "tom",
      //属性名和变量名相同时可省略变量名
      bar,
    }
    console.log(obj)    //{name: "tom", bar: "bar"}
    

    Object.assign

    const obj = {a:1,b:2}
    const obj1 = Object.assign({},obj)
    obj1.a=11
    console.log(obj)    //{a: 1, b: 2}
    console.log(obj1)    //{a: 11, b: 2}
    

    Object.is

    0 == false // true
    0 === false // false
    +0 === -0 // true
    NaN === NaN //false
    Object.is(+0,-0) // false
    Object.is(NaN,NaN) // true
    

    Class 类

    class Person {
      constructor (name, age) {
        this.name = name;
        this.age = age;
      }
      sayHi () {
        console.log(`hi,my name is ${this.name}`)
      } 
      //类方法,this指向的当前的类
      static create (name,age) {
        return new Person(name,age)
      }     
    }
    const p1 = new Person("tom",18)
    //调用类方法直接创建实例对象
    const p2 = Person.create("zs",19)
    console.log(p1)    //Person {name: "tom", age: 18}
    console.log(p2)    //Person {name: "zs", age: 19}
    -------------------------------------------------------------------------------
    //子类
    class Student extends Person {
      constructor (name,age,number) {
        //调用父类构造方法
        super(name,age)
        this.number = number
      }
      hello () {
        //调用父类函数
        super.sayHi()
        console.log(`学号是 ${this.number}`)
      }
    }
    const s1 = new Student("tom",18,101)
    s1.hello()    //hi,my name is tom       学号是101
    

    set数据结构

    // 创建数据结构
    const s = new Set()
    s.add(1).add(2).add(3).add(4).add(2)
    console.log(s)     //Set(4) {1, 2, 3, 4}
    //遍历
    s.forEach(i => console.log(i))    //1 2 3 4
    for (let i of s) {
       console.log(i)    //1 2 3 4
    }
    //获得集合的长度
    console.log(s.size)    //4
    //是否存在某个值
    console.log(s.has(4))    //true
    //删除某个值
    console.log(s.delete(100))    //打印false, s不变
    console.log(s.delete(3))    //打印true, Set(3) {1, 2, 4}
    //清空集合
    s.clear()    //Set(0) {}
    

    Map 数据结构

    const map = new Map()
    const a = { a: 1}
    const b = 123
    map.set(a,100)
    map.set(b,200)
    //获取对应的值
    console.log(map.get(a))  //100
    //map.has() 是否包含某个数据
    //map.delete() 删除
    // map.clear() 清空
    map.forEach((value,key) => {
      console.log(key,value)      //{a: 1} 100 ; 123 200
    })
    

    数组includes方法

    const arr = [1,true,NaN,23,'hello']
    console.log(arr.indexOf(NaN))    //-1 无法查找NaN的位置
    console.log(arr.includes(NaN))    //ture
    

    指数运算

    console.log(2 ** 3)    //8  (2的3次方)
    

    相关文章

      网友评论

        本文标题:ES6常用特性与一些注意点

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