js-new-对象干了啥

6次阅读

共计 373 个字符,预计需要花费 1 分钟才能阅读完成。

1、创建一个空对象,并且 this 变量引用该对象,// lat target = {};

2、继承了函数的原型。// target.proto = func.prototype;

3、属性和方法被加入到 this 引用的对象中。并执行了该函数 func// func.call(target);

4、新创建的对象由 this 所引用,并且最后隐式的返回 this。// 如果 func.call(target) 返回的 res 是个对象或者 function 就返回它

function new(func) {

lat target = {};
target.__proto__ = func.prototype;
let res = func.call(target);
if (typeof(res) == "object" || typeof(res) == "function") {return res;}
return target;

}

正文完
 0