代码实现构造函数中的new关键字
//代码实现new
function New(func){
console.log(func);
//创建一个空对象
let res={}
//对象的__proto__指向构造函数的prototype
res.__proto__=func.prototype
//ret为返回值,返回值如果不是引用数据类型,返回res
//返回值如果是引用数据类型 返回ret
let ret=func.apply(res,Array.prototype.slice.call(arguments,1))
if((typeof ret=='object'||typeof ret=='function')&&typeof ret!=null){
return ret
}
return res
}
function Student(name,age){
this.name=name;
this.age=age
}
Student.prototype.study=function(){
console.log('study');
}
console.log(Student.prototype);
// let boy=new Student('zhangsan',10)
// console.log(boy);
let boy=New(Student,'zhangsan',10)
console.log(boy);