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

我的个人博客 https://www.xuehuayu.cn

点击查看源码

		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();

你可能感兴趣的:(js,面试)