我可不可以直接操作__proto__呢

来源:3-13 面向对象(二)

qq_朕知道了_1

2019-05-04

就像这样

  function Parent3() {
    console.log('parent3 instantiation');
    this.name = "parent3";
    this.clothes = ["上衣", "裤子", "鞋子", "帽子"];
  }

  Parent2.prototype.say = function () {
    console.log('I am ' + this.name);
  };

  function Child3() {
    Parent3.call(this);
    this.type = "child3";
  }

  Child3.prototype.sayType = function () {
    console.log('My type is ' + this.type);
  };
  Child3.prototype.__proto__ = Parent3.prototype;
写回答

1回答

快乐动起来呀

2019-05-05

不可以的

0
2
weibo_普通cp3_0
回复
qq_朕知道了_1
因为“=”这样写是地址引用。object.create是重新创建来一个对象,把parent的原型copy进去,然后在赋值给child,可以实现不引用同一个地址
2020-02-12
共2条回复

前端跳槽面试必备技巧 面试官全流程指导

一门让你能成功获取前端 Offer 的课程, 不只是技术,面试“技巧”也至关重要

4836 学习 · 830 问题

查看课程