美文网首页
this 类型

this 类型

作者: 幸宇 | 来源:发表于2022-04-19 19:22 被阅读0次

    // this 类型
    // 类的成员方法可以直接返回一个 this,这样就可以很方便地实现链式调用。
    // 链式调用

    class StudyStep{
    step1(){
    console.log('step1')
    return this
    }
    step2(){
    console.log('step2')
    return this
    }
    }
    const s = new StudyStep()
    // s.step1().step2() // 链式调用
    class Mystep extends StudyStep{
    next(){
    console.log('next===')
    return this
    }
    }
    const m = new Mystep()
    // 父类型和子类型上的方法都可随意调用
    m.step1().next().step2().next()
    // 这样就保持了父类和子类之间接口调用的连贯性

    相关文章

      网友评论

          本文标题:this 类型

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