new一个对象时发生的几件事
function Keith() {
this.height = 180;
}
let boy = new Keith();
console.log(boy.height); //180
//let boy = Keith();
//console.log(boy.height); //如果没有new,则会输出undefined,因为此时boy中的this是指向全局的
- 创建一个boy的空对象,作为返回的实例,此时boy还是undefined
- 将boy的____proto____指向构造函数的prototype,也就是Keith的prototype
- 让构造函数内的this指针指向boy实例
- 开始指向构造函数内的语句
- 返回对象
这篇文章不错:
https://juejin.cn/post/6844903456478593031