手写简易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()
原文地址:https://www.cnblogs.com/styleFeng/p/14193555.html