1

every post, book, about javascript inheritance, tells us to fix the prototype constructor, after this situtation:

A.prototype = Object.create(B.prototype);
A.prototype.constructor = A ; //fixing here I undestand what 

I understand what Object.create does, however the next line seems to be useless. I wrote some code without it and that has no effect. So my question is, why everybody says to fix the constructor ?

Bruno Mateus
  • 1,727
  • 18
  • 25
  • 1
    That is like calling the super class constructor automatically from sub class. – Tushar Nov 20 '15 at 17:53
  • I'm exactly doing this, first doing a `V.Class.construct(V.Visual = function (config) { //...`, then doing `new V.Visual()`... see also: [V javaScript](http://metadea.de/V/) – metadings Nov 20 '15 at 17:57
  • 2
    odd - normally you'd assign `A.prototype.constructor = A`, to reattach the original constructor function `A` back to its prototype (having overwritten the entire prototype in the first line). You'd then do `B.call(this)` in the first line of `A()`. – Alnitak Nov 20 '15 at 17:57
  • @Alnitak is right, check for example this question: http://stackoverflow.com/questions/21069971/javascript-extend-objects-and-prototypes So maybe Bruno not all the world says what you claim. Where did you find it? – gsamaras Nov 20 '15 at 17:58
  • the `V.Visual function` also SHOULD do `V.Class.call(this, config)` – metadings Nov 20 '15 at 17:59
  • @aspillers - I think this is a premature close of the question - in this case the constructor has been overridden with the _superclass_ object, not the current classes. That _may_ just be an error by the OP, though. – Alnitak Nov 20 '15 at 18:02
  • 1
    To be clear, the reason you reattach back to `A` (and not `B`) is because when you subsequently construct an `A` with the code you quoted it would appear as an object of class `B` instead of an `A` in your debugger. – Alnitak Nov 20 '15 at 18:04
  • @gsamaras i was claiming about to have to set the prototype constructor and on the question you cited, the guy call the constructor when he is setting the prototype, so to build a new object the constructor will be called two times. – Bruno Mateus Nov 22 '15 at 22:20

0 Answers0