471,602 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,602 software developers and data experts.

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

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)

May 11 '06 #1
4 3761
VK

ingoweiss wrote:
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);

May 11 '06 #2

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

May 11 '06 #3
Julian Turner wrote:
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.
May 12 '06 #4

Richard Cornford wrote:
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

May 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Brent | last post: by
reply views Thread by flupke | last post: by
2 posts views Thread by ToChina | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.