Wednesday, 2 October 2013

JavaScript inheritance through prototype

JavaScript inheritance through prototype

There is the most popular JavaScript method for inheritance:
function extend(Child, Parent) {
var F = function() { }
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype;
}
I'm learning JS now, but I have a lot of Java experience and this
inheritance model seems some diffucult for me. Why I can't do the
following thing:
function extend(Child, Parent) {
Child.prototype = Parent.prototype;
Child.prototype.constructor = Child;
}
I think I don't know/understand some things, but F object is useless in my
opinion. Please, clarify the situation. Thanks.

No comments:

Post a Comment