手写简易new

记录贴

 

 

    // 手写new
    
    function Mynew(Fn,...args){
        if(typeof Fn !== "function"){
            throw "必须是方法体"
        }
        
        const obj = {}
        obj.__proto__ =  Object.create(Fn.prototype)
        Fn.apply(obj,args)
        return obj
    }
    
    function Ps(name,age){
        this.name = name
        this.age = age
        Ps.prototype.go = function(){
            console.log("原型")
        }
    }
    
    let vs = Mynew(Ps,"哈哈",2)
    console.log(vs.name)
    vs.go()

 

posted @ 2020-12-26 17:32  JS-Feng  阅读(108)  评论(0编辑  收藏  举报