//使用原型工厂封装继承
function extend(sub,sup){
    sub.prototype=Object.create(sup.prototype);
    Object.defineProperty(sub.prototype,"constructor",{
        value:sub,
        enumerable:false
    });
}

function User(name,age){
    this.name=name;
    this.age=age;
}

User.prototype.show=function(){
    console.log(this.name,this.age);
}

function Admin(...args){
    User.apply(this,args);
}

extend(Admin,User);
// let admin=new Admin('xj',12);
// admin.show();

//对象工厂派生对象并实现继承
function admin(name,age){
    const instance=Object.create(User.prototype);
    User.call(instance,name,age);
    instance.role=function(){
        console.log("role");
    }
    return instance;
}

let hd =admin('jack',12);
hd.role();

 

posted on 2022-01-29 20:00  weakup  阅读(23)  评论(0编辑  收藏  举报