原型对象(三)原型继承与原型链

原型继承

    // ES5继承:prototype
    function User(username,password){
        this.username = username
        this.password = password
        this.login = function () {
            console.log("登录")
        }
    }

    function Admin(){
        this.deletePerson = function(){
            console.log("删除一个用户")
        }
    }

    Admin.prototype = new User()
    let admin = new Admin
    admin.login()
原型链
    // 原型链 
    function User(username,password){
        this.username = username
        this.password = password
    }

    function Admin(){
        this.deletePerson = function(){
            console.log("删除一个用户")
        }
    }

    Object.prototype.login = function() {
        console.log("object原型上的登录方法")
    }
    Admin.prototype = new User()
    let admin = new Admin
    admin.login()
posted @ 2021-07-23 15:22  `Duet`  阅读(28)  评论(0编辑  收藏  举报