实现new功能函数这里?result 输出对象前面为什么有个Fun ?-灵析社区

一只臭美的Doggg

function _new(constructor, ...arg) { let obj = {}; console.log("constructor", constructor); console.log("constructor.prototype", constructor.prototype); obj.__proto__ = constructor.prototype; let res = constructor.apply(obj, arg); console.log("res", res); console.log("obj", obj); return Object.prototype.toString.call(res) === '[object object]' ? res : obj; } const Fun = function (name) { this.name = name; } console.log('result',_new(Fun, 'xyy')); ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/61291f630b7e0633557ef83c42cb3c08.png) ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250103/9e23ef681d27a9268efd14ad2ed58f56.png)

阅读量:272

点赞量:13

问AI
codbad
"image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20241222/19fd41cf88a386e8fb236291b88db6d8.png) 如图所示,只要这个对象有原型,就会显示构造函数名称