- 实现new效果
function Person(name, age) {
this.name = name;
this.age = age;
}
let per = new Person('name',18);
console.log(per);
// 实现 new
function myNew(fn, ...arg) {
// 1.声明一个空对象
let obj = {};
// 2.继承函数的prototype
obj.__proto__ = fn.prototype;
// 3.调用该函数并修改this指向
fn.apply(obj, arg);
// 4.返回此对象
return obj;
}
console.log(new myNew(Person, 'nn', 18));