请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

关于组合方法基继承

function Parent4 () {

          this.name = 'parent4';

          this.play = [1, 2, 3];

      }

      function Child4 () {

          Parent4.call(this);

          this.type = 'child4';

      }

      Child4.prototype = Parent4.prototype;

      var s5 = new Child4();

      var s6 = new Child4();

      console.log(s5, s6);


      console.log(s5 instanceof Child4, s5 instanceof Parent4);

      console.log(s5.constructor);

老师,最后这里得到s5的构造函数不应该是 s5.__proto__.constructor 吗,为什么s5.constructor也可以呢



正在回答 回答被采纳积分+3

2回答

假凤眼 2018-03-03 17:26:09

我认为是s5里没有constructor属性,然后顺着__proto__向上找,找到s5的构造函数的原型对象,原型对象里有constructor属性,s5.__proto__.constructor===s5.constructor了。

0 回复 有任何疑惑可以回复我~
JTR354 2017-08-13 11:18:48

s5.__protp__ === Child4.prototype

s5.__proto__.constructor === Child4.prototype.constructor

s5.constructor === Child4.prototype.constructor

所以,s5.constructor === s5.__proto__.constructor

我是这样想的,请老师指点.

0 回复 有任何疑惑可以回复我~
  • 提问者 百兽凯多00 #1
    s5.constructor === Child4.prototype.constructor 这个是为什么呢
    回复 有任何疑惑可以回复我~ 2017-08-13 12:17:44
  • s5 下面没有 constructor 属性,你之所以能访问到,那是通过 s5.__proto__. constructor 找到的。
    
    你在控制台把  s5 打印出来看一下(慕课没法贴图,要不可以画张图给你看)
    回复 有任何疑惑可以回复我~ 2017-08-13 12:21:31
  • 推荐篇文章 http://ntesmailfetc.blog.163.com/blog/static/2062870612012418102658176/
    回复 有任何疑惑可以回复我~ 2017-08-13 12:23:49
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信