修改__proto__为什么prototype也变了?

作者站长头像
站长
· 阅读数 21

看代码

function Parent() {
  this.name = "zhangsan";
  this.list = [];
}
function Child() {
  Parent.call(this);
  this.age = 18;
}
Child.prototype.__proto__=Parent.prototype;
Child.prototype.contructor = Child;

上面是继承的常见写法,默认情况下Child.prototype.__proto__应该指向Object.prototype,为了实现child是继承parent的,所以修改原型链Child.prototype.__proto__=Parent.prototype;到这我是理解的。

但是为什么修改了__proto__,Child.prototype也变成了Parent呢?__proto__不应该是prototye的一个属性吗?prototype的指向不应该还是Child吗,只是proto指向Parent来实现继承?

回复
1个回答
avatar
test
2024-07-16

修改__proto__不会发生你所说的问题,如果你说的是在控制台打印,前面有写一个Parent,那只是浏览器实现的,对象的类型标识,就是它的构造函数

answer image

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容