You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function_new(){constConstructor=Array.prototype.shift.call(arguments)constobj={}obj.__proto__=Constructor.prototypeconstret=Constructor.apply(obj,arguments)returntypeofret==='object' ? ret : obj}
Activity
vivimee commentedon Jun 28, 2020
new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。new 关键字会进行如下的操作:
Leecason commentedon Jun 28, 2020
ningtiao commentedon Jun 28, 2020
CreatorMr commentedon Jun 28, 2020
function _new(Fn, ...args) {
let obj = Object.create(Fn.prototype);
let res = Func.call(obj, ...args);
if(res !== null && /^(object|function)$/.test(typeof res) return res;
return obj;
}
xyj626553989 commentedon Jun 28, 2020
mrandpz commentedon Jun 28, 2020
lxhguard commentedon Jun 29, 2020
步骤:创建,执行,原型链,判断。
meta07 commentedon Jun 29, 2020
AsimoLoveGym commentedon Nov 3, 2020
@sisterAn 最后优化的版本 变量 Con的声明漏掉了let关键词
lixingyang-lxy commentedon May 10, 2021
Con.apply(obj, arguments); 这一步是什么意思呀?
xllpiupiu commentedon Oct 6, 2021
GreatAuk commentedon Oct 14, 2021
因为 Parent 没有返回值,所以
const result = Parent.apply(child, rest);的 result 永远是 undefinedeverthis commentedon Nov 8, 2021
huangyingjie commentedon Dec 10, 2022
不相信有人不查阅资料一次能写出来,目前来看最后一步这个写法最科学。
有同学使用
Object.prototype.toString.call,但是忽略了,它的返回值不一定是[object Object],比如Array,比如更改了Symbol.toStringTag的对象。总之还是太卷了