美文网首页es6
learn-es6常用语法(4)

learn-es6常用语法(4)

作者: crayona | 来源:发表于2019-11-19 17:30 被阅读0次

    1字符串新增方法

    模板字符串

    ${变量}

     ``    `解决之前字符串连接要多次拼接的问题

    新增方法  startsWith('http') 查找字符串开头是否包括

    endsWith('.com')  查找字符串结束是否包括

    includes('') 查找字符串中是否包含

    repeat()  参数重复的次数

    trimStart()消除头部空格  (es2019新增)

    trimEnd()消除尾部空格(es2019新增)

    ---------------------------------------------------

    class 对象

    函数声明  fn1 function(){ console.log(111)}

    函数表达式  let fn=function(){ console.log(111)}

    class man={

    constructor(){  //默认函数 在对象创建时自动调用

    }

    }

    static  关键字  (私有方法)

    (只有当前原型才可以执行该方法)

    (this指向当前function不是当前的对象)

    (不可以被继承)

    class man{

    constructor(name,age){this.name=name;this.age=age}

    getName(){console.log(this.name)}

    static getAge(){console.log(this.age)}   //this指function

    }//不可继承static   this指向function   只有原型才能调用该私有函数  (原型可以调用静态方法 比如static  实例才可以调用非静态方法)

    extends 继承

    class supperman  extends man{

    constructor(){

    super() //继承父类的构造函数  并且把构造的原型指向子类

    //代码等同于  man.prototype.constructor.call(this)

    }

    }

    super对象指的就是父级 可以用super对象调用 父级的静态方法

    class supperman extends man{

    constructor(){

    super()}

    fngetage(){   super.getAge()}   //利用super对象获得父级的static方法 getAge

    }

    ------------------

    可以通过 set  get方法来监听赋值、读取操作

    class  num{

    constructor(valuezhi){this.valuezhi=valuezhi}

    get getValue(){  if(typeof(this.valuezhi)!="number"){return "啦啦啦"}}

    set setValue(val){

    if(typeof(val)!="string"){ this.valuezhi="非字符串"}

    else{this.valuezhi=val}

    }

    }

    let numb=new num();

    numb.setValue='123';

    console.log(numb.zhi)  //字符串123

    console.log(numb.getValue)  //啦啦啦

    相关文章

      网友评论

        本文标题:learn-es6常用语法(4)

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