/**
new -
1.新建obj = {}
2.prototype 指向构造函数prototype
3.执行构造函数, bind this
4.返回, 如果构造函数有返回对象,则返回该对象; 否则返回新建对象
*/
function myNew(constructor, ...arguments) {
let obj = {}
Object.setPrototypeOf(obj,constructor.prototype)
console.log(arguments);
//this bind: apply bind obj 新对象
let other = constructor.apply(obj, arguments)
console.log(other);
return typeof other === 'object' ? other : obj;
}
function Product(name,price) {
this.name = name
this.price = price
// 返回对象 yu 返回 非对象区别
return {
'hero': 'hh'
}
}
let p = myNew(Product, 'jm',20)
console.log(p );