Considering the simplistic scenario:
function Base() {} function Child() {} Child.prototype = new Base;
I wonder why would an instance of Child have constructor property set to Base and not to Child?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s all tied to how inheritance works in JavaScript. Check this link for a detailed explanation (basically,
constructoris just another property of the prototype).Edit: Also, if you want ‘real’ prototypical inheritance, you have to use some sort of clone function, eg
Then, you can do things like this
and you’ll still get
truetwo times onThe difference to your solution is that
Base()won’t be called andSub.prototypewill only inherit the properties ofBase.prototype– and not the ones set in the constructor.