javascript-手写new操作符

loading 2022年11月22日 157次浏览

关于创建new时发生了什么可以看这篇文章

因此这里直接给出手写new代码和讲解

        function myNew(func){
            //创建一个空对象
            let obj = {};
            //将实例proto指向构造函数原型
            if(func.prorotype !== null){
                obj.__proto__ = func.prorotype;
            }
            //将构造函数内部的this指向obj
            //slice不从下标0开始取是因为myNew的第一个参数是fn
            let res = func.apply(obj , Array.from(arguments).slice(1));
            //如果函数没有返回对象类型就返回obj,否则返回res
            if(res !== null && (typeof res === 'object' || typeof res === 'function')){
                return res;
            }else{
                return obj;
            }            
        }
        
        function fn(name , age){
            this.name = name;
            this.age = age;
        }
        let obj = myNew(fn , 'yoimiya' , 21);
        console.log(obj);//{name:'yoimiya' age:21}

"如果函数没有返回对象类型就返回obj,否则返回res",这句直接看起来可能没那么好理解,看了例子就会很好理解:

        class Person {
            constructor(override) {
                this.foo = 'foo';
                if (override) {
                    return {
                        bar: 'bar'
                    };
                }
            }
        }
        let p1 = new Person(),
            p2 = new Person(true);

        console.log(p1); // Person{ foo: 'foo' } 
        console.log(p1 instanceof Person); // true 

        console.log(p2); // { bar: 'bar' } 
        console.log(p2 instanceof Person); // false