views:

203

answers:

2

so I'm finally understanding prototype and how to use it. I'm sure that I'm still trying to solve this as a java inheritance problem, so if there is a more prototypal way to go about this let me know.

If B inherits A I want B's constructor to first execute A's constructor. This is important for setting up B's local variables. At first I thought of doing something like

function B()
{
    B.prototype.constructor();
}
B.prototype = new A();

This of course does not work properly as it is essentially the same as saying

function B()
{
    new A();
}

Is there any way to actually extend a constructor in the java way? Is there a better way to go about this?

{EDIT} I realized the problem was that I was trying to access private variables. I'm assuming private variables do not get passed down from prototype?

+2  A: 

When you copy the A object into B's prototype you call A's constructor:

B.prototype = new A();

To setup A's local variables through the constructor arguments, you could do this:

B.prototype = new A(arguments, here);

Not sure if this answers your question though.

Luca Matteis
A: 

You can use any Ajax framework that supports classes emulation, they handle classes and inheritance in javascript very well. Also take a look at JS.Class.

the_drow
Not sure about "any Ajax framework." jQuery doesn't add classes to JavaScript.
Nosredna
Ok good to know. I never used jQuery.
the_drow