0
点赞
收藏
分享

微信扫一扫

new一个对象时发生的事情

向上的萝卜白菜 2022-04-08 阅读 105

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是指向全局的
  1. 创建一个boy的空对象,作为返回的实例,此时boy还是undefined
  2. 将boy的____proto____指向构造函数的prototype,也就是Keith的prototype
  3. 让构造函数内的this指针指向boy实例
  4. 开始指向构造函数内的语句
  5. 返回对象

这篇文章不错:

https://juejin.cn/post/6844903456478593031

举报

相关推荐

0 条评论