472,984 Members | 1,981 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 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 3845
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
0
by: flupke | last post by:
I have the following test code setup, trying to get the class name of a subclass in the super class. (Reason why i want this is described below) file class_name_start.py ========================...
4
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
2
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
5
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for...
1
by: shivapadma | last post by:
1..In java we can refer superclass constructor by super()keyword,but Where as in c++ how can we refer to that???. In java the following code is used for referring superclass...
2
by: Mark Brading | last post by:
Hi all, I am trying to declare a Map object with an abstract superclass (TableFields_v2) object as follows:- private Map<Integer, TableFields_v2> elementList; When I come to create the object...
3
by: bassman2112 | last post by:
I'm having issues with calling a method defined in a subclass on a superclass object. This program is an exercise using inheritance, with an Employee superclass, Salaried and Hourly classes that...
1
by: jimismint | last post by:
Hi guys, i'm stuck on this problem. only just recently started coding java using blue J. Can you guys help me. import java.util.*;...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.