Javascript仿写new运算符,new的运行原理

<script>
    function myNew(constructor, ...args){
        let obj = {};
        constructor.call(obj, ...args);
        obj.__proto__ = constructor.prototype;
        return obj;
    }

    function myClass(){
        this.name = 'Do';
    }

    let obj = myNew(myClass);
    console.log(obj.name);
</script>

猜你喜欢

转载自www.cnblogs.com/xieyicn/p/11685366.html