为什么我们需要在util.inherit中设置ctor.prototype.constructor = ctor?

时间:2018-06-18 21:09:22

标签: javascript node.js prototypal-inheritance

我正在查看util.inherit中的节点here。我还没有理解为什么在以下代码中authod执行此操作

  

ctor.prototype.constructor = ctor

此处还提供了从相同网址获取的代码段以及我的理解:

function (ctor, superCtor) {//ctor->Sub Object/Class,superCtor->SuperObject/Class
  ctor.super_ = superCtor //Holding Ref For Super Constructor function if we need to invoke super function using call/apply
  function F () {} //Creating blank Constructor function,We can add any member here without changing/impacting superCtor.prototype
  F.prototype = superCtor.prototype //Make Super Constructor prototype as Temp constructor prototype,so that we can inherit from super via this prototype link
  ctor.prototype = new F //Making The Chain,When we create sub object using ctor,created object will have temp blank object as prototype,which in turn have prototype pointing to superCtor prototype 
  ctor.prototype.constructor = ctor //??
}

现在我知道完成所有这些链接后,因为我们将temp new F设置为使用new ctor创建的所有对象的原型对象,ctor.prototype.constructor将不存在。这是我们在这里设置它的唯一原因,所以我们可以引用为ctor创建所有对象的构造函数吗? 你能引导我找到这个理由吗?

0 个答案:

没有答案