美文网首页
如何用es5实现继承

如何用es5实现继承

作者: 潘杉杉_06_03 | 来源:发表于2018-12-14 21:19 被阅读25次

    extend (继承)

    如何用 es5 实现继承

    Father.prototype = {
        eat:function(){
            console.log(this.name+' eat something.')
        }
    }
    
    function Father(name) {
        this.name = name
        this.attr = "father's attr."
    }
    function Super() {
        this.constructor = Child
    }
    Super.prototype = Father.prototype
    Child.prototype = new Super() // 继承父类的原型方法
    function Child() {
        Father.apply(this, arguments) // 继承父类的属性
        this.attr = "child's attr"
    }
    

    测试

    var child = new Child('Foo')
    
    console.log(child,child.attr)
    
    console.log(child instanceof Child, child instanceof Father)
    
    child.eat()
    
    console.log(child.newAttr)
    
    Father.prototype.newAttr = '123'
    
    console.log(child.newAttr)
    
    console.log(Child.prototype.constructor === Child)
    

    结果

    Child { name: 'Foo', attr: 'child\'s attr' } 'child\'s attr'
    true true
    Foo eat something.
    undefined
    123
    true
    

    原文链接

    相关文章

      网友评论

          本文标题:如何用es5实现继承

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