Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 11th, 2006, 07:35 AM
ingoweiss
Guest
 
Posts: n/a
Default 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)


  #2  
Old May 11th, 2006, 10:05 AM
VK
Guest
 
Posts: n/a
Default Re: trouble passing parameters of a subclass constructor through to it's superclass constructor


ingoweiss wrote:[color=blue]
> 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)[/color]

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);

  #3  
Old May 11th, 2006, 12:15 PM
Julian Turner
Guest
 
Posts: n/a
Default Re: trouble passing parameters of a subclass constructor through to it's superclass constructor


ingoweiss wrote:

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

[snip][color=blue]
> Derived.prototype.constructor = Derived;[/color]
[/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

  #4  
Old May 12th, 2006, 05:05 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: trouble passing parameters of a subclass constructor through to it's superclass constructor

Julian Turner wrote:[color=blue]
> ingoweiss wrote:
> [snip][color=green]
>> function Derived(a)
>> {
>> this.prototype.constructor.call(a);
>> }[/color]
> [/snip]
>
> [snip][color=green]
>> Derived.prototype.constructor = Derived;[/color]
> [/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.[/color]

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.


  #5  
Old May 12th, 2006, 07:45 AM
Julian Turner
Guest
 
Posts: n/a
Default Re: trouble passing parameters of a subclass constructor through to it's superclass constructor


Richard Cornford wrote:
[color=blue]
> 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.[/color]
[color=blue]
> 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.[/color]

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.