0
function extend(Child, Parent){
    var F = function() { }
    F.prototype = Parent.prototype
    Child.prototype = new F()
    Child.prototype.constructor = Child
    Child.superclass = Parent.prototype
}

I have a problem, when extending the parent class the children prototype functions are being deleted, how to save the children functions and extend the parent using this function?

Hello
  • 2,247
  • 4
  • 23
  • 29
  • I believe you're profoundly confused on the nature of prototype. Please read this: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work It will help you start unwinding the mess you have in your question. – Yevgeny Simkin Apr 29 '13 at 05:21
  • it looks like I can extend using this function only one time, because the prototype overwrites all the data in the children prototype – Hello Apr 29 '13 at 05:25
  • prototype doesn't work the way you think it works. Please familiarize yourself with how to use it. Notice how in the example I link to, no one is assigning anything TO prototype. That's because that's not what it's there for. `Child.prototype = new Foo()` is a horrible mistake. – Yevgeny Simkin Apr 29 '13 at 05:43
  • Why shouldn't you assign to `prototype`? How else would you achieve prototype chaining? – a better oliver Apr 29 '13 at 06:20

1 Answers1

0

When you do something like that then you would add the properties of the child prototype afterwards.

function MyChild() {}
extend(MyChild, MyParent);

MyChild.prototype.newMethod = function () {}

I suggest using Object.create or a sham.

a better oliver
  • 26,330
  • 2
  • 58
  • 66