trouble passing parameters of a subclass constructor through to it's superclass constructor

I

ingoweiss

Hi,

I am having trouble passing parameters of a Javascript subclass
constructor through to it's superclass constructor.

I am trying all sorts of things, including the below, but nothing
worked so far.

Thanks for any help!




function Base(a)
{
this.a = a;
}

function Derived(a)
{
this.prototype.constructor.call(a);
}

Derived.prototype = new Base();
Derived.prototype.constructor = Derived;

var derived = new Derived("hello");
//a should be "hello" now, but it is undefined:
window.alert(derived.a)
 
V

VK

ingoweiss said:
Hi,

I am having trouble passing parameters of a Javascript subclass
constructor through to it's superclass constructor.

I am trying all sorts of things, including the below, but nothing
worked so far.

Thanks for any help!




function Base(a)
{
this.a = a;
}

function Derived(a)
{
this.prototype.constructor.call(a);
}

Derived.prototype = new Base();
Derived.prototype.constructor = Derived;

var derived = new Derived("hello");
//a should be "hello" now, but it is undefined:
window.alert(derived.a)

Be careful with an a la class inheritance in the conventional
JavaScript (thus not JScript.Net and such). Be careful exactly because
it's an "a la class inheritance", not "the class inheritance".
Everything can work pretty close to what you may used to in other
languages, but many features are emulated or missing.
And for sure you shouldn't use both prototype inheritance and class
inheritance together. Fists of all it's a needless mess, secondly it's
error prone, finally (as you just discovered) it simply doesn't work in
the way you think it should work.

// by keeping (a la) class inheritance:

function Base(a) {
this.foo = 'bar';
this.a = a;
}

function Derived(a,b) {
Base.call(this,a);
this.b = b;
}

var obj = new Derived('a','b');
alert(obj.foo);
alert(obj.a);
alert(obj.b);
 
J

Julian Turner

ingoweiss wrote:

[snip]
function Derived(a)
{
this.prototype.constructor.call(a);
} [/snip]

[snip]
Derived.prototype.constructor = Derived;
[/snip]

The last line of code sets the value of the "constructor" property of
"Derived.prototype" as a reference to "Derived".

So in your expression "this.prototype.constructor.call", you calling
Derived, not Base.

Regards

Julian Turner
 
R

Richard Cornford

Julian said:
ingoweiss wrote:
[snip]
function Derived(a)
{
this.prototype.constructor.call(a);
} [/snip]

[snip]
Derived.prototype.constructor = Derived;
[/snip]

The last line of code sets the value of the "constructor"
property of "Derived.prototype" as a reference to "Derived".

So in your expression "this.prototype.constructor.call",
you calling Derived, not Base.

Not really. When - new Derived("hello"); - is executed the - this -
keyword refers to the object being constructed. That object is not a
function. It does not have a - prototype - property, so no function is
called because of the run-time error generated when -
this.prototype.constructor - is referenced.

If the function was called it seems that it would be the wrong function,
but passing in the string primitive "hello" to the function's - call -
method would be worthless as a temporary Sting object should be created
for it, passed into the method to act as the - this - object in the
function call, and then be thrown away as no references to it would
remain.

Richard.
 
J

Julian Turner

Richard said:
Not really. When - new Derived("hello"); - is executed the - this -
keyword refers to the object being constructed. That object is not a
function. It does not have a - prototype - property,
called because of the run-time error generated when -
this.prototype.constructor - is referenced.
If the function was called it seems that it would be the wrong function,
but passing in the string primitive "hello" to the function's - call -
method would be worthless as a temporary Sting object should be created
for it, passed into the method to act as the - this - object in the
function call, and then be thrown away as no references to it would
remain.

Thank you, yes I forgot those two points. I was too focused on the
fact that the "constructor" property of Derived's prototype was not
even pointing to Base, let alone other problems with the code.

Indeed, if the OP had omitted the "prototype" from the statement, and
properly included a reference to an object in the "call" method thus:-

"this.constructor.call(this,a);"

a stack overflow would have obviously resulted.

The OP may (I am guessing) have been thinking of a pattern along the
lines of the Java "super" keyword, as an alternative to the patten
suggested by VK:-

function Base(a)
{
this.myProp=a;
}

function Derived(a)
{
/* this.superClassConstructor.call(this,a) */
/* or more simply ... */

this.superClassConstructor(a);
}

Derived.prototype=new Base();
Derived.prototype.constructor=Derived;
Derived.prototype.superClassConstructor=Base;

Regards

Julian Turner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top