【JS面试题】原生JS手写new方法

		function Parent() {
            this.name = "parent";
            this.say = function() {
                console.log("say")
            }
        }

        Parent.prototype = {
            walk: function() {
                console.log("walk")
            }
        }

        function newfn(fn) {
            if (typeof fn !== 'function') return;
            let son = {};
            fn.call(son);
            fn.prototype.constructor = fn;
            son.__proto__ = fn.prototype;
            return son;
        }
        let son1 = newfn(Parent);
        son1.walk();

猜你喜欢

转载自blog.csdn.net/u010762099/article/details/88977807
今日推荐