Also is this a good inheritance pattern? I never saw it being discussed in JavaScript inheritance tutorials, yet sometimes being used in actual code. Can you point out drawbacks and/or advantages over other patterns?
Asked
Active
Viewed 69 times
0
-
1*"Can you point out drawbacks and/or advantages over other patterns?"* See my [answer here](http://stackoverflow.com/a/17393153/218196). – Felix Kling Jan 26 '15 at 22:20
-
@FelixKling Was waiting for your reaction: Hurray for the God. – Mouser Jan 26 '15 at 22:21
-
@FelixKling thanks, great answer, very thorough. Still, it doesn't seem to address why one needs `B.prototype.constructor = B` here, does one? There is always also the actual constructor on B which was implicitly created by defining B in the first place, right? So the B.prototype.constructor should never be used anyway, or am I missing something? – hubzkq1 Jan 26 '15 at 22:33
-
Right, but that was not the scope of the question / answer ;) The only reason why one does that is to set the value of `constructor` back to its original value, before `B.prototype` was modified. – Felix Kling Jan 26 '15 at 22:35
-
@FelixKling it seems redundant and unnecessary, or am I missing something? – hubzkq1 Jan 26 '15 at 22:37
-
The `constructor` property is arguably a lesser used feature, however, if you intend your code to play well with other 3rd party code it's better to correct the value. You can't know whether the other code makes use of `constructor` in one way or the other. – Felix Kling Jan 26 '15 at 22:39
-
Constructor is used by the chrome developer tools and can be used by the programmer (this.constructor) you should not create an instance of Parent to be used as prototype of Child. It shows a lack of understanding of the roles the constructor and prototype play. More info here: http://stackoverflow.com/a/16063711/1641941 – HMR Jan 27 '15 at 00:56
-
@HMR thanks for pointing me to this very thorough answer :) – hubzkq1 Feb 01 '15 at 22:53
1 Answers
0
Because the constructor is now pointing to the constructor(prototype property) of "A" so we want it to point it to B()
B.prototype.constructor is an pointer to the original constructor when we change the prototype of B (B.prototype = new A()
) the B.prototype.constructor loses the reference to actual A() and refers to/points to B(). Note new A() will still call default constructor.
Here only purpose to change it back to B() (B.prototype.constructor=B
) can be that when in future for some purpose calling the constructor with prototype like B.prototype.constructor()
or B.prototype.constructor.call()
it doesn't point to A()
Example
function A(){
console.log("A")
}
function B(){
console.log("B")
}
B.prototype = Object.create(A.prototype);
new B() // Still console.logs "B"
B.prototype.constructor() // console.logs "A"

A.B
- 20,110
- 3
- 37
- 71
-
-
that doesn't matter though, does it? Because there is also the actual constructor on B which was implicitly created by defining B in the first place. So the B.prototype.constructor should never be used anyway, or am I missing something? – hubzkq1 Jan 26 '15 at 22:34
-
Although it wont effect as default constructor will be called but it is good to make it point back to constructor from protype object also – A.B Jan 26 '15 at 22:41
-